c# 发送邮件帮助类

 

发送邮件帮助类:

  1 using System;
  2 using System.Net.Mail;
  3 using System.Text;
  4 
  5 namespace PersonBlog.Tools
  6 {
  7 
  8     public class EmailHelper
  9     {
 10 
 11         #region Eail 属性
 12 
 13         private string _mailFrom = "";
 14         /// <summary>
 15         /// 发送者
 16         /// </summary>
 17         public string mailFrom { get { return _mailFrom; } set { _mailFrom = value; } }
 18 
 19         /// <summary>
 20         /// 收件人
 21         /// </summary>
 22         public string[] mailToArray { get; set; }
 23 
 24         /// <summary>
 25         /// 抄送
 26         /// </summary>
 27         public string[] mailCcArray { get; set; }
 28 
 29         /// <summary>
 30         /// 标题
 31         /// </summary>
 32         public string mailSubject { get; set; }
 33 
 34         /// <summary>
 35         /// 正文
 36         /// </summary>
 37         public string mailBody { get; set; }
 38 
 39         /// <summary>
 40         /// 发件人密码
 41         /// </summary>
 42         public string mailPwd { get; set; }
 43 
 44         private string _host = "";
 45         /// <summary>
 46         /// SMTP邮件服务器
 47         /// </summary>
 48         public string host { get { return _host; } set { _host = value; } }
 49 
 50         private bool _isbodyHtml = true;
 51         /// <summary>
 52         /// 正文是否是html格式
 53         /// </summary>
 54         public bool isbodyHtml { get { return _isbodyHtml; } set { _isbodyHtml = value; } }
 55 
 56         private string _nickname = "博客 系统通知";
 57         /// <summary>
 58         /// 发送者昵称
 59         /// </summary>
 60         public string nickname
 61         {
 62             get { return _nickname; }
 63             set
 64             {
 65                 _nickname = value;
 66             }
 67         }
 68 
 69         /// <summary>
 70         /// 附件
 71         /// </summary>
 72         public string[] attachmentsPath { get; set; }
 73 
 74         //优先级别
 75         private MailPriority _Priority = MailPriority.Normal;
 76         /// <summary>
 77         /// 优先级别  默认正常优先级
 78         /// </summary>
 79         public MailPriority Priority
 80         {
 81             get
 82             {
 83                 return _Priority;
 84             }
 85             set
 86             {
 87                 _Priority = value;
 88             }
 89         }
 90         #endregion
 91 
 92         #region 发送邮件方法
 93         public void Send()
 94         {
 95             //使用指定的邮件地址初始化MailAddress实例
 96             MailAddress maddr = new MailAddress(mailFrom, nickname);
 97             //初始化MailMessage实例
 98             MailMessage myMail = new MailMessage();
 99 
100             //向收件人地址集合添加邮件地址
101             if (mailToArray != null)
102             {
103                 for (int i = 0; i < mailToArray.Length; i++)
104                 {
105                     myMail.To.Add(mailToArray[i].ToString());
106                 }
107             }
108 
109             //向抄送收件人地址集合添加邮件地址
110             if (mailCcArray != null)
111             {
112                 for (int i = 0; i < mailCcArray.Length; i++)
113                 {
114                     myMail.CC.Add(mailCcArray[i].ToString());
115                 }
116             }
117             //发件人地址
118             myMail.From = maddr;
119 
120             //电子邮件的标题
121             myMail.Subject = mailSubject;
122 
123             //电子邮件的主题内容使用的编码
124             myMail.SubjectEncoding = Encoding.UTF8;
125 
126             //电子邮件正文
127             myMail.Body = mailBody;
128 
129             //电子邮件正文的编码
130             myMail.BodyEncoding = Encoding.Default;
131 
132             //邮件优先级
133             myMail.Priority = Priority;
134 
135             myMail.IsBodyHtml = isbodyHtml;
136 
137             //在有附件的情况下添加附件
138             try
139             {
140                 if (attachmentsPath != null && attachmentsPath.Length > 0)
141                 {
142                     Attachment attachFile = null;
143                     foreach (string path in attachmentsPath)
144                     {
145                         attachFile = new Attachment(path);
146                         myMail.Attachments.Add(attachFile);
147                     }
148                 }
149             }
150             catch (Exception err)
151             {
152                 throw new Exception("在添加附件时有错误:" + err);
153             }
154 
155             SmtpClient smtp = new SmtpClient();
156             //指定发件人的邮件地址和密码以验证发件人身份
157             smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);//115                 //设置SMTP邮件服务器
158             smtp.Host = host;
159             // smtp.EnableSsl = true;
160             //smtp.Port = 587;
161             //将邮件发送到SMTP邮件服务器
162             smtp.Send(myMail);
163         }
164         #endregion
165     }
166 }

 

调用:

 1 EmailHelper email = new EmailHelper()
 2 {
 3 mailPwd = "sqm0626",
 4 host = "smtp.163.com",
 5 mailFrom = "xxx@163.com",
 6 mailSubject = "系统提示",
 7 mailBody = "邮箱邮箱验证码",
 8 mailToArray = new string[] { "957608954@qq.com" }
 9 };
10 try
11 {
12 email.Send();
13 Response.Write("发送成功");
14 }
15 catch (Exception ex)
16 {
17 
18 Response.Write("发送失败:" + ex);
19 }

转自:http://www.cnblogs.com/zhaopei/p/4770340.html

c# 发送邮件帮助类

上一篇:net core webapi jwt


下一篇:基于Postman的API自动化测试