码迷,mamicode.com
首页 > 其他好文 > 详细

C# 发送邮件

时间:2016-03-09 15:28:56      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

今天研究了下C#发邮件的代码,主要是参考别人的,原文:http://www.cnblogs.com/xuchi/archive/2011/12/15/2288663.html

 1      /// <summary>
 2         /// 发送邮件
 3         /// </summary>
 4         /// <param name="smtp">发件服务器</param>
 5         /// <param name="pwd">发件人密码</param>
 6         /// <param name="sendMail">发件人邮箱</param>
 7         /// <param name="recipientMail">收件人邮箱</param>
 8         /// <param name="sendUsername">发送人姓名</param>
 9         /// <param name="recipientUsername">收件人姓名</param>
10         /// <param name="Subject">邮件主题</param>
11         /// <param name="Body">邮件内容</param>
12         /// <param name="CC">抄送人</param>
13         /// <returns>是否发送成功</returns>
14         public static string sendMail(string smtp, string pwd, string sendMail, string recipientMail, string sendUsername, string recipientUsername, string Subject, string Body, string[] CC)
15         {
16             try
17             {
18                 SmtpClient client = new SmtpClient(smtp);//初始化服务器
19 
20                 client.UseDefaultCredentials = false;
21                 client.Credentials = new NetworkCredential(sendMail, pwd);//发送人的账户,密码
22                 client.DeliveryMethod = SmtpDeliveryMethod.Network;//指定如何处理待发邮件
23 
24                 MailAddress addressFrom = new MailAddress(sendMail, sendUsername); //发件人地址,姓名
25                 MailAddress addressTo = new MailAddress(recipientMail, recipientUsername);//收件人地址,姓名
26 
27                 //Attachment att= new Attachment("");   // 附件
28 
29                 MailMessage message = new MailMessage(addressFrom, addressTo); //发信
30                 // 循环遍历抄送人
31                 foreach (var item in CC)
32                 {
33                     message.CC.Add(item);
34                 }                
35 
36                 message.Sender = new MailAddress(sendMail); // 发件人地址
37                 message.Subject = Subject;  // 主题
38                 message.Body = Body;    // 内容
39                 message.BodyEncoding = Encoding.UTF8;   // 编码
40                 message.IsBodyHtml = true;  // 是否为html格式
41 
42                 client.Send(message);
43                 return "true";
44             }
45             catch (Exception ex)
46             {
47                 return (ex.Message);
48             }
49         }

 

C# 发送邮件

标签:

原文地址:http://www.cnblogs.com/LoveMin/p/5258035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!