c# 发送邮件

发送邮件帮助类

 

  1 using System;
  2 using System.Net.Mail;
  3 using System.Text;
  4 
  5 namespace Wxlhjc.Public
  6 {
  7     public class SendEmail
  8     {
  9         /// <summary>
 10         /// 发送者
 11         /// </summary>
 12         public string mailFrom { get; set; }
 13 
 14         /// <summary>
 15         /// 收件人
 16         /// </summary>
 17         public string[] mailToArray { get; set; }
 18 
 19         /// <summary>
 20         /// 抄送
 21         /// </summary>
 22         public string[] mailCcArray { get; set; }
 23 
 24         /// <summary>
 25         /// 标题
 26         /// </summary>
 27         public string mailSubject { get; set; }
 28 
 29         /// <summary>
 30         /// 正文
 31         /// </summary>
 32         public string mailBody { get; set; }
 33 
 34         /// <summary>
 35         /// 发件人密码
 36         /// </summary>
 37         public string mailPwd { get; set; }
 38 
 39         /// <summary>
 40         /// SMTP邮件服务器
 41         /// </summary>
 42         public string host { get; set; }
 43 
 44         /// <summary>
 45         /// 正文是否是html格式
 46         /// </summary>
 47         public bool isbodyHtml { get; set; }
 48 
 49         /// <summary>
 50         /// 附件
 51         /// </summary>
 52         public string[] attachmentsPath { get; set; }
 53 
 54         public bool Send()
 55         {
 56             //使用指定的邮件地址初始化MailAddress实例
 57             MailAddress maddr = new MailAddress(mailFrom);
 58             //初始化MailMessage实例
 59             MailMessage myMail = new MailMessage();
 60 
 61 
 62             //向收件人地址集合添加邮件地址
 63             if (mailToArray != null)
 64             {
 65                 for (int i = 0; i < mailToArray.Length; i++)
 66                 {
 67                     myMail.To.Add(mailToArray[i].ToString());
 68                 }
 69             }
 70 
 71             //向抄送收件人地址集合添加邮件地址
 72             if (mailCcArray != null)
 73             {
 74                 for (int i = 0; i < mailCcArray.Length; i++)
 75                 {
 76                     myMail.CC.Add(mailCcArray[i].ToString());
 77                 }
 78             }
 79             //发件人地址
 80             myMail.From = maddr;
 81 
 82             //电子邮件的标题
 83             myMail.Subject = mailSubject;
 84 
 85             //电子邮件的主题内容使用的编码
 86             myMail.SubjectEncoding = Encoding.UTF8;
 87 
 88             //电子邮件正文
 89             myMail.Body = mailBody;
 90 
 91             //电子邮件正文的编码
 92             myMail.BodyEncoding = Encoding.Default;
 93 
 94             myMail.Priority = MailPriority.High;
 95 
 96             myMail.IsBodyHtml = isbodyHtml;
 97 
 98             //在有附件的情况下添加附件
 99             try
100             {
101                 if (attachmentsPath != null && attachmentsPath.Length > 0)
102                 {
103                     Attachment attachFile = null;
104                     foreach (string path in attachmentsPath)
105                     {
106                         attachFile = new Attachment(path);
107                         myMail.Attachments.Add(attachFile);
108                     }
109                 }
110             }
111             catch (Exception err)
112             {
113                 throw new Exception("在添加附件时有错误:" + err);
114             }
115 
116             SmtpClient smtp = new SmtpClient();
117             //指定发件人的邮件地址和密码以验证发件人身份
118             smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);
119 
120 
121             //设置SMTP邮件服务器
122             smtp.Host = host;
123             smtp.EnableSsl = true;
124             try
125             {
126                 //将邮件发送到SMTP邮件服务器
127                 smtp.Send(myMail);
128                 return true;
129 
130             }
131             catch (System.Net.Mail.SmtpException ex)
132             {
133                 return false;
134             }
135 
136         }
137     }
138 }

 

调用方法

 

 1  /// <summary>
 2         /// 发送密码至邮件
 3         /// </summary>
 4         /// <param password="password">新密码</param>
 5         /// <returns></returns>
 6         public bool SendEmail(string appName, string emailId, string newPassWord)
 7         {
 8             SendEmail email = new SendEmail();
 9             email.mailFrom = "xxxx@163.com";
10             email.mailPwd = "*******";
11             email.mailSubject = "找回密码";
12             string url = "http:/baidu.com";
13             email.mailBody = "<div style='width:100%;height:200px;'><div style='font-size:15px;background-color:#FFF1B4;'>您好,您在:<span style='color:#428bca;'>" + appName + "</span>找回密码,请记住验证码:<span>" + newPassWord + ",验证码30分钟内有效。";
14             email.mailBody += "请打开此链接重设密码:<a href='" + url + "'>'" + url + "'</a>(如果无法点击该URL链接地址,请将它复制并粘帖到浏览器的地址输入框,然后单击回车即可。)</span></div></div>";
15             email.isbodyHtml = true;    //是否是HTML
16             email.host = "smtp.163.com";//如果是QQ邮箱则:smtp:qq.com,依次类推
17             email.mailToArray = new string[] { emailId };//接收者邮件集合
18             return email.Send();
19         }

 

posted @ 2018-11-30 10:53  这座城市那么空  阅读(176)  评论(0编辑  收藏  举报