在.NET framework的強大支持下,我們可以很容易的使用電子郵件發(fā)送這個功能,此功能實現(xiàn)的前提是你的郵箱必須開通Smtp功能
通常在一封郵件中,存在以下內(nèi)容:
From: 發(fā)送方地址
To: 接受方地址
BodyFormat: 正文內(nèi)容類型,我們這里采用HTML格式
BodyEncoding: 正文內(nèi)容編碼,這里采用的是默認編碼: DEFAULT
Subject: 信件的主題,即標題
Body: 信件的內(nèi)容,即正文
SmtpServer: SMTP 服務(wù)器的地址,用來發(fā)送電子郵件
下面的代碼片斷說明了如何使用該功能
using System.web.Mail;
...
MailMessage msg = new MailMessage();
//發(fā)送方地址
msg.From = "[email protected]";
//接收方地址
msg.To = "[email protected]";
//正文內(nèi)容類型
msg.BodyFormat = MailFormat.Html;
//正文內(nèi)容編碼
msg.BodyEncoding = System.Text.Encoding.Default;
//主題
msg.Subject = "LIUCSOFT向您問好";
//內(nèi)容
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>";
//設(shè)置為需要用戶驗證
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//設(shè)置驗證用戶名
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "liucsoft");
//設(shè)置驗證密碼
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123456");
//郵件服務(wù)器地址(如smtp.163.com)
SmtpMail.SmtpServer = "smtp.163.com";
//發(fā)送
SmtpMail.Send(msg);
...
新聞熱點
疑難解答