在.NET framework的強大支持下,我們可以很容易的使用電子郵件發送這個功能,此功能實現的前提是你的郵箱必須開通Smtp功能
通常在一封郵件中,存在以下內容:
From: 發送方地址
To: 接受方地址
BodyFormat: 正文內容類型,我們這裡採用HTML格式
BodyEncoding: 正文內容編碼,這裡採用的是默認編碼: DEFAULT
Subject: 信件的主題,即標題
Body: 信件的內容,即正文
SmtpServer: SMTP 服務器的地址,用來發送電子郵件
下面的代碼片斷說明瞭如何使用該功能
using System.web.Mail;
...
MailMessage msg = new MailMessage();
//發送方地址
msg.From = [email protected];
//接收方地址
msg.To = [email protected];
//正文內容類型
msg.BodyFormat = MailFormat.Html;
//正文內容編碼
msg.BodyEncoding = System.Text.Encoding.Default;
//主題
msg.Subject = LIUCSOFT向您問好;
//內容
msg.Body = <html><head><META content=zh-cn http-equiv=Content-Language><meta http-equiv='Content-Type' content='text/html; charset=gb2312'></ head><body>這是一封測試郵件,不必回复</body></html>;
//設置為需要用戶驗證
msg.Fields.Add(http://schemas.microsoft.com/cdo/configuration/smtpauthenticate, 1);
//設置驗證用戶名
msg.Fields.Add(http://schemas.microsoft.com/cdo/configuration/sendusername, liucsoft);
//設置驗證密碼
msg.Fields.Add(http://schemas.microsoft.com/cdo/configuration/sendpassword, 123456);
//郵件服務器地址(如smtp.163.com)
SmtpMail.SmtpServer = smtp.163.com;
//發送
SmtpMail.Send(msg);
...