一、介绍
, T9 A {7 o# m5 a; G( @ System.Net.Mail命名空间是在.NET Framework中新增的,该命名空间提供了发送电子邮件的功能。通过对本章的学习,读者可以轻松地使用.NET Framework提供的类库来发送电子邮件。System.Net.Mail 命名空间包含用于将电子邮件发送到SMTP服务器的类,这些类需要结合Microsoft SMTP Server一起使用。
7 {5 X7 b) I0 X# } System.Net.Mail 命名空间下有SmtpClient类用于发送邮件,可以完全代替SmtpMail类。利用SmtpClient类的Send方法可以完成发送电子邮件的传 输,也可以用SendAsync方法进行异步发送,后者发送完成后会产生一个SendCompleted 事件来通知发送结果。Send方法可以接受MailMessage类的对象作为参数。通过MailMessage类可以设置邮件更多的内容和格式,例如,为 Attachment类设置添加附件的参数。
+ @0 [4 P" w# n3 \% M+ G( G SmtpClient 类与SMTP结合在一起,通过MailMessage类、MailAddress类、Attachment类来丰富电子邮件的内容和设置。图18-2展示 了用户通过System.Net.Mail命名空间下的类结合SMTP发送电子邮件的过程。
% H, b) B2 R. P# N3 `# J二、SmtpClient类的语法定义如下:
6 V6 q6 W# G0 p* b. J9 m9 ipublic class SmtpClient1 I$ X" a: @2 z" z' N+ k
下面的代码演示如何创 建一个SmtpClient的实例。
7 q0 ?1 B0 g1 SSmtpClient client = new SmtpClient (“smtp.Sina.com”); //直接通过构造函数设置SMTP 主机服务器
+ m7 G7 d5 D" k% c' U# v或:
' x2 G2 q1 K- q( C4 rSmtpClient client = new SmtpClient ();9 L: g: c% S' W1 `
Client. Host =” smtp.Sina.com”; //通过Host属性来设置SMTP 主机服务器
5 c' k3 O7 A' L+ n+ w- N# C三、完整代码- @' _5 j' u3 b5 b) n ?7 C
- /// <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">发件人</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>
- /// 发送邮件
- /// </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 "发件人为空";
- }
- if (string.IsNullOrWhiteSpace(password))
- {
- return "发件人密码为空";
- }
- 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();
- }
|