介紹
System.Net.Mail命名空間是在.NET Framework中新增的,該命名空間提供了發(fā)送電子郵件的功能。通過對本章的學習,讀者可以輕松地使用.NET Framework提供的類庫來發(fā)送電子郵件。System.Net.Mail 命名空間包含用于將電子郵件發(fā)送到SMTP服務器的類,這些類需要結合Microsoft SMTP Server一起使用。
System.Net.Mail 命名空間下有SmtpClient類用于發(fā)送郵件,可以完全代替SmtpMail類。利用SmtpClient類的Send方法可以完成發(fā)送電子郵件的傳 輸,也可以用SendAsync方法進行異步發(fā)送,后者發(fā)送完成后會產生一個SendCompleted 事件來通知發(fā)送結果。Send方法可以接受MailMessage類的對象作為參數。通過MailMessage類可以設置郵件更多的內容和格式,例如,為 Attachment類設置添加附件的參數。
SmtpClient 類與SMTP結合在一起,通過MailMessage類、MailAddress類、Attachment類來豐富電子郵件的內容和設置。圖18-2展示 了用戶通過System.Net.Mail命名空間下的類結合SMTP發(fā)送電子郵件的過程。
SmtpClient類的語法定義如下:
public class SmtpClient
下面的代碼演示如何創(chuàng) 建一個SmtpClient的實例。
SmtpClient client = new SmtpClient (“smtp.Sina.com”); //直接通過構造函數設置SMTP 主機服務器
或:
SmtpClient client = new SmtpClient ();
Client. Host =” smtp.Sina.com”; //通過Host屬性來設置SMTP 主機服務器
完整代碼
/// <summary>
/// 郵件處理器
/// </summary>
public class MailHandler
{
private MailMessage _mailMessage;
private string _host;
private string _userName;
private string _password;
public MailHandler()
{
}
/// <summary>
/// 設置郵件信息
/// </summary>
/// <param name="subject">主體</param>
/// <param name="body">內容</param>
/// <param name="from">發(fā)件人</param>
/// <param name="to">收件人</param>
/// <param name="cc">抄送人</param>
/// <param name="bcc">密件抄送人</param>
/// <param name="isBodyHtml">內容是否為Html</param>
public void SetMailMessage(string subject, string body, string from, string[] to, string[] cc, string[] bcc, bool isBodyHtml = true)
{
_mailMessage = new MailMessage();
_mailMessage.Subject = subject;
_mailMessage.Body = body;
_mailMessage.IsBodyHtml = isBodyHtml;
_mailMessage.From = new MailAddress(from);
if (to != null)
{
foreach (var item in to)
{
_mailMessage.To.Add(item);
}
}
if (cc != null)
{
foreach (var item in cc)
{
_mailMessage.CC.Add(item);
}
}
if (bcc != null)
{
foreach (var item in bcc)
{
_mailMessage.Bcc.Add(item);
}
}
_mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
}
/// <summary>
/// 配置Smtp服務主機及身份驗證
/// </summary>
/// <param name="host">Smtp主機名或Ip</param>
/// <param name="userName">用戶名</param>
/// <param name="password">密碼</param>
public void SetSmtp(string host, string userName, string password)
{
this._host = host;
this._userName = userName;
this._password = password;
}
/// <summary>
/// 發(fā)送郵件
/// </summary>
public void Send()
{
using (var sc = new SmtpClient())
{
sc.Host = _host;
sc.Port = 25;
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.Credentials = new System.Net.NetworkCredential(_userName, _password);
sc.Send(_mailMessage);
}
}
public string SendMail(string title, string content)
{
var smptHost = ConfigHelper.GetAppSetting("SmtpHost");
var userName = ConfigHelper.GetAppSetting("MailUserName");
var password = ConfigHelper.GetAppSetting("MailPassword");
var mailToAddress = ConfigHelper.GetAppSetting("MailAddress").Split(',');
if (string.IsNullOrWhiteSpace(smptHost))
{
return "SmtpHost為空";
}
if (string.IsNullOrWhiteSpace(userName))
{
return "發(fā)件人為空";
}
if (string.IsNullOrWhiteSpace(password))
{
return "發(fā)件人密碼為空";
}
if (mailToAddress.Length == 0)
{
return "收件人列表為空";
}
var mailContent = @"<html><head><title>郵件內容</title></head>
<body>" + content + "</body></html>";
SetSmtp(smptHost, userName, password);
SetMailMessage(title, mailContent, userName, mailToAddress, null, null);
try
{
Send();
}
catch (Exception ex)
{
return ex.Message;
}
return null;
}
}
該文章在 2024/3/19 11:40:59 編輯過