教育改变生活 选择决定命运

.Net培训:.Net使用SmtpClient发送邮件

时间2017-08-01 03:18    来源:北大青鸟昌平校区

  北大青鸟北京昌平校区老师解读:.Net使用SmtpClient发送邮件

  从网上找了一个封装。Net的Smtp邮件发送的类

  [csharp]

  class MailSender

  {

  private MailMessage mailMessage;

  private SmtpClient smtpClient;

  private string password;//发件人密码

  ///

  /// 处审核后类的实例

  ///

  /// 收件人地址

  /// 发件人地址

  /// 邮件正文

  /// 邮件的主题

  /// 发件人密码

  public MailSender(string To, string From, string Body, string Title, string Password)

  {

  mailMessage = new MailMessage();

  mailMessage.To.Add(To);

  mailMessage.From = new System.Net.Mail.MailAddress(From);

  mailMessage.Subject = Title;

  mailMessage.Body = Body;

  mailMessage.IsBodyHtml = true;

  mailMessage.BodyEncoding = System.Text.Encoding.UTF8;

  mailMessage.Priority = System.Net.Mail.MailPriority.Normal;

  this.password = Password;

  }

  ///

  /// 添加附件

  ///

  public void Attachments(string Path)

  {

  string[] path = Path.Split(',');

  Attachment data;

  ContentDisposition disposition;

  for (int i = 0; i < path.Length; i++)

  {

  data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化附件

  disposition = data.ContentDisposition;

  disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取附件的创建日期

  disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//获取附件的修改日期

  disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附件的读取日期

  mailMessage.Attachments.Add(data);//添加到附件中

  }

  }

  ///

  /// 异步发送邮件

  ///

  ///

  public void SendAsync(SendCompletedEventHandler CompletedMethod)

  {

  if (mailMessage != null)

  {

  smtpClient = new SmtpClient();

  smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据

  smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

  smtpClient.Host = "smtp." + mailMessage.From.Host;

  smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//注册异步发送邮件完成时的事件

  smtpClient.SendAsync(mailMessage, mailMessage.Body);

  }

  }

  ///

  /// 发送邮件

  ///

  public void Send()

  {

  if (mailMessage != null)

  {

  smtpClient = new SmtpClient();

  smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据

  smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

  smtpClient.Host = "smtp." + mailMessage.From.Host;

  smtpClient.Send(mailMessage);

  }

  }

  }

  class MailSender

  {

  private MailMessage mailMessage;

  private SmtpClient smtpClient;

  private string password;//发件人密码

  ///

  /// 处审核后类的实例

  ///

  /// 收件人地址

  /// 发件人地址

  /// 邮件正文

  /// 邮件的主题

  /// 发件人密码

  public MailSender(string To, string From, string Body, string Title, string Password)

  {

  mailMessage = new MailMessage();

  mailMessage.To.Add(To);

  mailMessage.From = new System.Net.Mail.MailAddress(From);

  mailMessage.Subject = Title;

  mailMessage.Body = Body;

  mailMessage.IsBodyHtml = true;

  mailMessage.BodyEncoding = System.Text.Encoding.UTF8;

  mailMessage.Priority = System.Net.Mail.MailPriority.Normal;

  this.password = Password;

  }

  ///

  /// 添加附件

  ///

  public void Attachments(string Path)

  {

  string[] path = Path.Split(',');

  Attachment data;

  ContentDisposition disposition;

  for (int i = 0; i < path.Length; i++)

  {

  data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化附件

  disposition = data.ContentDisposition;

  disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取附件的创建日期

  disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//获取附件的修改日期

  disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附件的读取日期

  mailMessage.Attachments.Add(data);//添加到附件中

  }

  }

  ///

  /// 异步发送邮件

  ///

  ///

  public void SendAsync(SendCompletedEventHandler CompletedMethod)

  {

  if (mailMessage != null)

  {

  smtpClient = new SmtpClient();

  smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据

  smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

  smtpClient.Host = "smtp." + mailMessage.From.Host;

  smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//注册异步发送邮件完成时的事件

  smtpClient.SendAsync(mailMessage, mailMessage.Body);

  }

  }

  ///

  /// 发送邮件

  ///

  public void Send()

  {

  if (mailMessage != null)

  {

  smtpClient = new SmtpClient();

  smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据

  smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

  smtpClient.Host = "smtp." + mailMessage.From.Host;

  smtpClient.Send(mailMessage);

  }

  }

  }

  使用某些邮箱的时候会报错(如sohu):不允许使用邮箱名称,服务器响应为: xxx.需要设置单独的用户名,故修改了下,并做了一些优化。如下:

  [csharp]

  class MailSender

  {

  private readonly MailMessage mail;

  private readonly string password;//发件人密码

  private readonly string user;

  ///

  /// 处审核后类的实例

  ///

  /// 收件人地址

  /// 发件人地址

  /// 邮件正文

  /// 邮件的主题

  /// 发件人账号

  /// 发件人密码

  public MailSender(string to, string from, string body, string subject, string sendAccount, string sendPassword)

  {

  mail = new MailMessage();

  mail.To.Add(to);

  mail.From = new MailAddress(from);

  mail.Subject = subject;

  mail.Body = body;

  mail.IsBodyHtml = true;

  mail.BodyEncoding = System.Text.Encoding.UTF8;

  mail.Priority = MailPriority.Normal;

  this.password = sendPassword;

  this.user = sendAccount;

  }

  ///

  /// 添加附件

  ///

  public void Attachments(string path)

  {

  string[] pathes = path.Split(',');

  for (int i = 0; i < pathes.Length; i++)

  {

  var data = new Attachment(pathes[i], MediaTypeNames.Application.Octet);//实例化附件

  var disposition = data.ContentDisposition;

  disposition.CreationDate = System.IO.File.GetCreationTime(pathes[i]);//获取附件的创建日期

  disposition.ModificationDate = System.IO.File.GetLastWriteTime(pathes[i]);//获取附件的修改日期

  disposition.ReadDate = System.IO.File.GetLastAccessTime(pathes[i]);//获取附件的读取日期

  mail.Attachments.Add(data);//添加到附件中

  }

  }

  ///

  /// 异步发送邮件

  ///

  ///

  public void SendAsync(SendCompletedEventHandler CompletedMethod)

  {

  if (mail != null)

  {

  var smtpClient = new SmtpClient();

  smtpClient.Credentials = new System.Net.NetworkCredential(user, password);//设置发件人身份的票据

  smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

  smtpClient.Host = "smtp." + mail.From.Host;

  smtpClient.SendCompleted += CompletedMethod;//注册异步发送邮件完成时的事件

  smtpClient.SendAsync(mail, mail.Body);

  }

  }

  ///

  /// 发送邮件

  ///

  public void Send()

  {

  if (mail != null)

  {

  var smtpClient = new SmtpClient();

  smtpClient.Credentials = new System.Net.NetworkCredential(user, password);//设置发件人身份的票据

  smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

  smtpClient.Host = "smtp." + mail.From.Host;

  smtpClient.Send(mail);

  }

  }

  }

  class MailSender

  {

  private readonly MailMessage mail;

  private readonly string password;//发件人密码

  private readonly string user;

  ///

  /// 处审核后类的实例

  ///

  /// 收件人地址

  /// 发件人地址

  /// 邮件正文

  /// 邮件的主题

  /// 发件人账号

  /// 发件人密码

  public MailSender(string to, string from, string body, string subject, string sendAccount, string sendPassword)

  {

  mail = new MailMessage();

  mail.To.Add(to);

  mail.From = new MailAddress(from);

  mail.Subject = subject;

  mail.Body = body;

  mail.IsBodyHtml = true;

  mail.BodyEncoding = System.Text.Encoding.UTF8;

  mail.Priority = MailPriority.Normal;

  this.password = sendPassword;

  this.user = sendAccount;

  }

  ///

  /// 添加附件

  ///

  public void Attachments(string path)

  {

  string[] pathes = path.Split(',');

  for (int i = 0; i < pathes.Length; i++)

  {

  var data = new Attachment(pathes[i], MediaTypeNames.Application.Octet);//实例化附件

  var disposition = data.ContentDisposition;

  disposition.CreationDate = System.IO.File.GetCreationTime(pathes[i]);//获取附件的创建日期

  disposition.ModificationDate = System.IO.File.GetLastWriteTime(pathes[i]);//获取附件的修改日期

  disposition.ReadDate = System.IO.File.GetLastAccessTime(pathes[i]);//获取附件的读取日期

  mail.Attachments.Add(data);//添加到附件中

  }

  }

  ///

  /// 异步发送邮件

  ///

  ///

  public void SendAsync(SendCompletedEventHandler CompletedMethod)

  {

  if (mail != null)

  {

  var smtpClient = new SmtpClient();

  smtpClient.Credentials = new System.Net.NetworkCredential(user, password);//设置发件人身份的票据

  smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

  smtpClient.Host = "smtp." + mail.From.Host;

  smtpClient.SendCompleted += CompletedMethod;//注册异步发送邮件完成时的事件

  smtpClient.SendAsync(mail, mail.Body);

  }

  }

  ///

  /// 发送邮件

  ///

  public void Send()

  {

  if (mail != null)

  {

  var smtpClient = new SmtpClient();

  smtpClient.Credentials = new System.Net.NetworkCredential(user, password);//设置发件人身份的票据

  smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

  smtpClient.Host = "smtp." + mail.From.Host;

  smtpClient.Send(mail);

  }

  }

  }

北大青鸟热门高薪就业岗位
北大青鸟昌平校区-招生问答更多全部提问 >
北大青鸟昌平校区-资讯更多全部提问 >

相亲相爱一家人

北大青鸟文化艺术节之青鸟之歌

北大青鸟文化艺术节之酷炫

北大青鸟文化艺术节之俏黎竹

北大青鸟文化艺术节之变脸

适合学习的人群
直接问咨询师
刘亚老师

入职测试、职业能力测试。Benet5.0课程、网络营销课程

陈欣老师

入职测试、职业能力测试。Benet5.0课程、网络营销课程

她回答的问题 | 她咨询的学员
李玲老师

入职测试、职业能力测试。Benet5.0课程、网络营销课程

她回答的问题 | 她咨询的学员