代码改变世界

C# 使用SmtpClient发送Email

2017-06-09 16:32  skye_Jia  阅读(1811)  评论(0编辑  收藏  举报

使用Winfrom写的报错信息发送邮件通知。

以下主要代码

 1         /// <summary>
 2         /// 发送邮件核心代码
 3         /// </summary>
 4         /// <param name="oFFromMail">发送邮箱全地址</param>
 5         /// <param name="oFFromMailPwd">发送邮箱</param>
 6         /// <param name="oFDisName">与邮箱关联的关联显示名</param>
 7         /// <param name="toMail">收件人邮箱全地址</param>
 8         /// <param name="oFSubject">邮件标题</param>
 9         /// <param name="oFBody">邮件内容</param>
10         /// <returns>返回是否发送成功</returns>
11         private void sendMailMethod(string oFBody)
12         {
13             try
14             {
15                 string oFFromMail = config.AppSettings.Settings["Email"].Value;//123456@qq.com
16                 string oFFromMailPwd = config.AppSettings.Settings["Psd"].Value;//QQ密码
17                 string oFDisName = "邮件发送服务";
18                 string toMail = config.AppSettings.Settings["ToEmail"].Value;//接受的邮件地址 123321@qq.com
19                 string host = config.AppSettings.Settings["Host"].Value;//QQ是smtp.qq.com;其他网上百度
20 
21                 //以下设置服务器
22                 MailMessage mailmsg = new MailMessage();
23                 mailmsg.From = new MailAddress(oFFromMail, oFDisName);
24                 mailmsg.To.Add(toMail);//"bad.huan@163.com"
25                 mailmsg.Subject = "商品上架功能异常邮件";
26                 mailmsg.Body = "您好啊,这是测试邮件!<br />" + oFBody;
27                 mailmsg.BodyEncoding = System.Text.Encoding.UTF8;
28                 mailmsg.IsBodyHtml = true;
29 
30                 SmtpClient sendmsg = new SmtpClient(host);
31                 sendmsg.Host = host;
32                 33                 34                 
35 
36                 sendmsg.Credentials = new NetworkCredential(oFFromMail, oFFromMailPwd);
37                 sendmsg.EnableSsl = true;
38                 sendmsg.Send(mailmsg);
39                 mailmsg.Dispose();
40             }
41             catch (Exception ex)
42             {
43                 throw new Exception(ex.Message, ex);
44             }
45         }

以上就是我刚验证过的代码,有问题可向我提问哦