005. C#发送邮件

 1        /// <summary>
 2         /// 发送邮件
 3        /// </summary>
 4        /// <param name="toEmailS">邮件接收者列表</param>
 5        /// <param name="titleEmail">邮件标题</param>
 6        /// <param name="bodyEmail">邮件主体(内容)</param>
 7         public void SendMail(string strEmail, string titleEmail, string bodyEmail)
 8         {
 9             string from = "EmailName@163.com";  //指定发送方的账户名
10             string server = "smtp.163.com"; //指定发送方使用的邮件服务器
11             System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(server);
12             //指定发送方登录邮箱的用户名和密码
13             client.Credentials = new System.Net.NetworkCredential("EmailName", "EmailPwd");
14             //指定电子邮件的发送方式, 枚举值, 有三种方式, 这里使用网络方式
15             client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
16             string[] toEmailS = strEmail.Split(',');
17             foreach (string str in toEmailS)
18             {
19                 System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, str, titleEmail, bodyEmail);
20                 try
21                 {
22                     client.Send(message);
23                 }
24                 catch (Exception ex)
25                 {
26                     System.Windows.Forms.MessageBox.Show(ex.ToString());
27                     return;
28                 }
29 
30             }
31             client.Dispose(); //释放连接
32         }

 

posted on 2016-11-21 12:03  印子  阅读(195)  评论(0编辑  收藏  举报

导航