高软玩家

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 1  /// <summary>
 2         /// 发送邮件
 3         /// </summary>
 4         /// <param name="smtp">发送服务地址smtp.163.com</param>
 5         /// <param name="sendAdress">发送人邮件地址</param>
 6         /// <param name="sendAdressPwd">发送人邮箱密码</param>
 7         /// <param name="toAddress">接收人邮箱</param>
 8         /// <param name="subject">标题</param>
 9         /// <param name="bodyText">发送内容</param>
10         /// <param name="filePath">附件地址</param>
11         /// <returns></returns>13         public string SendMail(string smtp, string sendAdress, string sendAdressPwd, string toAddress, string subject, string bodyText, string[] filePath)
14         {
15             //发送按钮的单击事件
16 
17             try
18             {
19                 //确定smtp服务器地址。实例化一个Smtp客户端
20                 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtp);
21                 //生成一个发送地址
22                 string strFrom = string.Empty;
23 
24                 //构造一个发件人地址对象
25                 MailAddress from = new MailAddress(sendAdress, "888888", Encoding.UTF8);
26                 //构造一个收件人地址对象
27                 MailAddress to = new MailAddress(toAddress, strFrom, Encoding.UTF8);
28 
29                 //构造一个Email的Message对象
30                 MailMessage message = new MailMessage(from, to);
31                 if (filePath != null)
32                 {
33                     //为 message 添加附件
34                     foreach (string ss in filePath)
35                     {
36                         //得到文件名
37                         string fileName = ss;
38                         //判断文件是否存在
39                         if (File.Exists(fileName))
40                         {
41                             //构造一个附件对象
42                             Attachment attach = new Attachment(fileName);
43                             //     得到文件的信息
44                             ContentDisposition disposition = attach.ContentDisposition;
45                             disposition.CreationDate = System.IO.File.GetCreationTime(fileName);
46                             disposition.ModificationDate = System.IO.File.GetLastWriteTime(fileName);
47                             disposition.ReadDate = System.IO.File.GetLastAccessTime(fileName);
48                             //向邮件添加附件
49                             message.Attachments.Add(attach);
50                         }
51                         else
52                         {
53                             //MessageBox.Show("文件" + fileName + "未找到!");
54                         }
55                     }
56                 }
57 
58                 //添加邮件主题和内容
59                 message.Subject = subject;
60                 message.SubjectEncoding = Encoding.UTF8;
61                 message.Body = bodyText;
62                 message.BodyEncoding = Encoding.UTF8;
63 
64                 //设置邮件的信息
65                 client.DeliveryMethod = SmtpDeliveryMethod.Network;
66                 message.BodyEncoding = System.Text.Encoding.UTF8;
67                 message.IsBodyHtml = false;
68 
69                 ////如果服务器支持安全连接,则将安全连接设为true。
70                 ////gmail支持,163不支持,如果是gmail则一定要将其设为true
71                 //if (cmbBoxSMTP.SelectedText == "smpt.163.com")
72                 //    client.EnableSsl = false;
73                 //else
74                 //    client.EnableSsl = true;
75 
76                 //设置用户名和密码。
77                 //string userState = message.Subject;
78                 client.UseDefaultCredentials = false;
79                 string username = sendAdress;
80                 string passwd = sendAdressPwd;
81                 //用户登陆信息
82                 NetworkCredential myCredentials = new NetworkCredential(username, passwd);
83                 client.Credentials = myCredentials;
84                 //发送邮件
85                 client.Send(message);
86                 //提示发送成功
87                 return "成功";
88             }
89             catch (Exception ex)
90             {
91                 return "失败\r\n" + ex.ToString();
92             }
93 
94         }

 

posted on 2017-09-13 12:33  高软玩家  阅读(349)  评论(0编辑  收藏  举报