ASP.net下调用SmtpClient实现smtp方式邮件发送(带附件)

 

做asp.net的网站被要求要加入一个邮件自动发送的功能,同时还要加入附件网上搜索了很多,自己记录总结一下

废话说完先贴代码:

  1. public class MyMail  
  2.   {  
  3.       private MyMail(string title, string content, string from, string to)  
  4.       {  
  5.           this.Title = title;  
  6.           this.Content = content;  
  7.           this.SendFrom = from;  
  8.           this.SendTo = to;  
  9.           this.IsHTML = false;  
  10.           this.Priority = System.Net.Mail.MailPriority.Normal;  
  11.           this.Accessories = new List<string>();  
  12.       }  
  13.       /// <summary>   
  14.       /// 创建封邮件   
  15.        /// </summary>   
  16.       /// <param name="title">标题</param>   
  17.       /// <param name="content">正文</param>   
  18.       /// <param name="from">发件邮箱</param>   
  19.       /// <param name="to">收件邮箱</param>   
  20.       /// <returns></returns>   
  21.       public static MyMail CreateMail(  
  22.                     string title, string content, string from, string to)  
  23.       {  
  24.           return new MyMail(title, content, from, to);  
  25.       }  
  26.   
  27.       /// <summary>   
  28.       /// 添加附件   
  29.        /// </summary>   
  30.       /// <param name="path">附件存放路径</param>   
  31.       public void AddAccessory(string path)  
  32.       {  
  33.           this.Accessories.Add(path);  
  34.       }  
  35.   
  36.       /// <summary>   
  37.       /// 移除添加的附件   
  38.        /// </summary>   
  39.       /// <param name="path">附件存放路径</param>   
  40.       public void RemoveAccessory(string path)  
  41.       {  
  42.           this.Accessories.Remove(path);  
  43.       }  
  44.   
  45.       /// <summary>   
  46.       /// 邮件标题   
  47.       /// </summary>   
  48.       public string Title { getset; }  
  49.   
  50.       /// <summary>   
  51.       /// 正文内容   
  52.       /// </summary>   
  53.       public string Content { getset; }  
  54.   
  55.   
  56.   
  57.       /// <summary>   
  58.       /// 发件邮箱地址   
  59.       /// </summary>   
  60.       public string SendFrom { getset; }  
  61.   
  62.       /// <summary>   
  63.       /// 收件邮箱地址   
  64.       // </summary>   
  65.       public string SendTo { getset; }  
  66.   
  67.       /// <summary>   
  68.       /// 正文格式是否为Html   
  69.       /// </summary>   
  70.       public bool IsHTML { getset; }  
  71.   
  72.       /// <summary>   
  73.       /// 邮件优先级   
  74.       /// </summary>   
  75.       public System.Net.Mail.MailPriority Priority { getset; }  
  76.   
  77.       /// <summary>   
  78.       /// 携带附件存放路径   
  79.       /// </summary>   
  80.       public List<string> Accessories { getset; }  
  81.   }  
  82.   
  83.   public class MailServer  
  84.   {  
  85.       private MailServer(string server,string usr,string passwnd)  
  86.       {  
  87.           this.Port = 25;  
  88.           this.TimeOut=9999;  
  89.           this.ServerHost = server;  
  90.           this.UserName = usr;  
  91.           this.PassWnd = passwnd;  
  92.       }  
  93.   
  94.       /// <summary>   
  95.       /// 创建一个邮件发送服务   
  96.       /// </summary>   
  97.       /// <param name="server"服务></param>   
  98.       /// <param name="usr">账户登录名</param>   
  99.       /// <param name="passwnd">密码</param>   
  100.       /// <returns></returns>   
  101.       public static MailServer CreateMailServer(  
  102.                     string server, string usr, string passwnd)  
  103.       {  
  104.           return new MailServer(server, usr, passwnd);  
  105.       }  
  106.   
  107.       /// <summary>   
  108.       /// smtp服务地址   
  109.       /// </summary>   
  110.       public string ServerHost { getset; }  
  111.   
  112.       /// <summary>   
  113.       /// 登录用户名   
  114.       /// </summary>   
  115.       public string UserName { getset; }  
  116.   
  117.       /// <summary>   
  118.       /// 登录密码   
  119.       /// </summary>   
  120.       public string PassWnd { getset; }  
  121.   
  122.       /// <summary>   
  123.       /// 端口号默认不用设置为25   
  124.       /// </summary>   
  125.       public int Port { getset; }  
  126.   
  127.       /// <summary>   
  128.       /// 超时设定   
  129.       /// </summary>   
  130.       public int TimeOut { getset; }  
  131.   }  
  132.   
  133.   public class MailSend  
  134.   {  
  135.       protected System.Net.Mail.SmtpClient _client;  
  136.   
  137.       public MailSend(MailServer server)  
  138.       {  
  139.           _client= new System.Net.Mail.SmtpClient(server.ServerHost,server.Port);  
  140.           _client.Credentials = new System.Net.NetworkCredential(  
  141.               server.UserName,server.PassWnd);  
  142.           _client.Timeout = server.TimeOut;  
  143.       }  
  144.   
  145.       public bool Send(MyMail mymail)  
  146.       {  
  147.           try  
  148.           {  
  149.               //邮件发送人地址   
  150.               System.Net.Mail.MailAddress from =   
  151.                                         new System.Net.Mail.MailAddress(mymail.SendFrom);  
  152.               //收件人地址   
  153.               System.Net.Mail.MailAddress to =  
  154.                                         new System.Net.Mail.MailAddress(mymail.SendTo);  
  155.               System.Net.Mail.MailMessage mail =   
  156.                                         new System.Net.Mail.MailMessage(mymail.SendFrom, mymail.SendTo);  
  157.               mail.Subject = mymail.Title;  
  158.               mail.Body = mymail.Content;  
  159.               mail.IsBodyHtml = mymail.IsHTML;  
  160.               //添加附件   
  161.               foreach (string accessory in mymail.Accessories)  
  162.               {  
  163.                   System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(accessory);  
  164.                   mail.Attachments.Add(data);  
  165.               }  
  166.               _client.Send(mail);  
  167.               mail.Dispose();  
  168.               return true;  
  169.           }  
  170.           catch  
  171.           {  
  172.               return false;  
  173.           }  
  174.       }  
  175.   }  

附上三个类的类图:

 

       再给个实际调用的例子:

  1. MailServer server =   
  2.                  MailServer.CreateMailServer(  
  3.                       "smtp.163.com",   
  4.                       " sender@163.com ",   
  5.                       "******");//此处填写stmp服务提供商、登录帐号、密码   
  6.  MyMail mail=MyMail.CreateMail(  
  7.      "测试邮件标题",   
  8.      "你好这是一封测试邮件",   
  9.      "sender@163.com",  
  10.      "receiver@sina.com");          //此处填写邮件标题、正文、发送者邮箱和接受者邮箱   
  11.  mail.AddAccessory(@"E:\Winter.jpg");//此处用于添加本地路径附件   
  12.  MailSend mailsender = new MailSend(server);  
  13.  if (mailsender.Send(mail))  
  14.  {  
  15.      //邮件发送成功   
  16.  }  
  17.  else  
  18.  {  
  19.      //失败处理   
  20.  }  

注意:邮件发送者必须与服务提供商是同一个。例如:发送者邮箱为163的邮箱,登录名、密码和stmp服务提供商就必须是163的。

另外有些邮箱,例如qq邮箱必须先登录邮箱手动开启stmp服务!!!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值