云顶国际,云顶国际登录官网

    欢迎访问云顶国际登录官网~

  1. 加入收藏
  2. 设为主页
  3. 学校简介
北大青鸟

三分钟了解北大青鸟

×
  • 北大青鸟大数据课程
  • 北大青鸟网络工程师
  • 北大青鸟web设计专业
  • 北大青鸟PHP专业
  • 北大青鸟启蒙星课程

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

责任编辑:秩名来源:云顶国际登录官网.Net技术教程发布时间:2013-04-22 10:54
导读: 北大青鸟武汉宏鹏鲁广校区老师解读:.Net使用SmtpClient发送邮件

  北大青鸟武汉宏鹏鲁广校区老师解读:.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);

  }

  }

  }

本文标题:.Net培训:.Net使用SmtpClient发送邮件,责任编辑:秩名,来源:北大青鸟武汉宏鹏鲁广校,于2013年04月22日10时54分发布于云顶国际登录官网。云顶国际宏鹏鲁广校区,是北大青鸟武汉电脑学校,专业IT计算机培训,拥有软件/网络/安卓/Web前端/Java课程.北大青鸟地址:光谷广场鲁巷数码港.电话:027-87807717

专业老师指导

专业老师指导

赵老师

从事IT教育培训十年有余,致力于帮助广大学子找到适合自己的专业。

培训咨询客服

培训咨询客服

陈老师

IT培训专业客服,用自己的真诚解决了无数学子的困惑。

本文地址:
文章标题:
  • 北大青鸟Java软件开发
  • 北大青鸟UI设计课程_零基础可学
  • 北大青鸟动漫设计_武汉设计学校
  • 北大青鸟数字艺术_武汉电脑学校
在线咨询
×
在线咨询更多问题

如您有任何疑问
在线咨询随时为您解答或拨打咨询热线:17740513250

×
  • 姓 名*
  • 手 机*
  • QQ号/微信号
  • 所报学科 *
  • 其他备注
  • 温馨提示:请保持手机畅通,咨询老师将为您提供专属的一对一报名服务。