c#帮助类:发送邮件

2021-06-22 01:06

阅读:667

标签:add   sbo   odi   邮件   user   port   bool   code   catch   

 1         private static string IsOpenSendMail = ConfigurationManager.AppSettings["IsOpenSendMail"]; //是否启用异常消息发送邮箱功能
 2         private static string SendUser=ConfigurationManager.AppSettings["MailUserName"];
 3         private static string SendPass= ConfigurationManager.AppSettings["MailPassword"];
 4         private static string ReceiveMail = ConfigurationManager.AppSettings["ReceiveMail"];
 5         private static string MailHost = ConfigurationManager.AppSettings["MailHost"]; //smtp.gmail.com
 6         private static string MailPort = ConfigurationManager.AppSettings["MailPort"]; //Gmail的smtp端口587
 7         /// 
 8         /// 
 9         /// 
10         /// 标题
11         /// 内容
12         /// 编码
13         /// 是否Html
14         /// 是否SSL加密连接
15         /// 是否成功
16         /// 
17         public static void SendMail(string mailName, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = true)
18         {
19             try
20             {
21                 if (!string.IsNullOrEmpty(IsOpenSendMail) && IsOpenSendMail=="开启")
22                 {
23                     MailMessage message = new MailMessage();
24                     message.To.Add(new MailAddress(ReceiveMail)); // 接收人邮箱地址
25                     message.From = new MailAddress(SendUser, mailName);
26                     message.BodyEncoding = Encoding.GetEncoding(encoding);
27                     message.Body = body;
28                     //GB2312
29                     message.SubjectEncoding = Encoding.GetEncoding(encoding);
30                     message.Subject = mailName;
31                     message.IsBodyHtml = isBodyHtml;
32 
33                     SmtpClient smtpclient = new SmtpClient(MailHost, Convert.ToInt32(MailPort)); //Gmail的smtp端口587
34                     smtpclient.Credentials = new System.Net.NetworkCredential(SendUser, SendPass);
35                     smtpclient.EnableSsl = enableSsl; //Gmail要求SSL连接
36                     smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network; //Gmail的发送方式是通过网络的方式,需要指定
37                     smtpclient.Send(message);
38                 }
39             }
40             catch (Exception ex)
41             {
42             }
43         }

 

c#帮助类:发送邮件

标签:add   sbo   odi   邮件   user   port   bool   code   catch   

原文地址:https://www.cnblogs.com/qinyi173/p/10222900.html


评论


亲,登录后才可以留言!