C# 發送email郵件!

利用C#郵件發送郵箱使用到兩個類SmtpClient和MailMessage。能夠把SmtpClient看作發送郵件信息的客戶端,而把MailMessage看作須要發送的消息。html

下面是我寫的發送郵件的公共方法:安全

  1  /// <summary>
  2         /// 
  3         /// </summary>
  4         /// <param name="ServerIP">發送郵件服務器ip或者域名</param>
  5         /// <param name="ServerPort">服務器端口</param>
  6         /// <param name="UserName">發件人的帳號</param>
  7         /// <param name="Password">發件人的密碼</param>
  8         /// <param name="FromAddress">發件人的郵箱地址</param>
  9         /// <param name="FromAddressName">發件人名稱</param>
 10         /// <param name="Subject">主題</param>
 11         /// <param name="Body">正文</param>
 12         /// <param name="ToAddressList">收件人集合</param>
 13         /// <param name="CCAddressList">副本收件人集合</param>
 14         /// <param name="BccAddressList">密件抄送收件人集合</param>
 15         /// <param name="IsHtml">正文是否能夠使用html標籤</param>
 16         /// <param name="AttachMentFileList">附件</param>
 17         /// <param name="MailPriority">發送郵件的優先級 三個選項:低 中 高</param>
 18         /// <returns></returns>
 19         public String SendMail(string ServerIP, int? ServerPort, string UserName, string Password, string FromAddress, string FromAddressName, string Subject, string Body, List<string> ToAddressList, List<string> CCAddressList, List<string> BccAddressList, bool IsHtml, List<AttachmentFile> AttachMentFileList, MailPriority MailPriority)
 20         {
 21             try
 22             {
 23                 if (string.IsNullOrEmpty(ServerIP))
 24                 {
 25                     return "ServerIP 不能爲空!";
 26                 }
 27 
 28                 SmtpClient mSmtpClient = new SmtpClient(ServerIP); //申明SmtpClient對象
 29                 System.Net.Mail.MailMessage mMailMessage = new System.Net.Mail.MailMessage(); //申明MailMessage對象
 30 
 31                 if (ServerPort != null)
 32                 {
 33                     mSmtpClient.Port = (int)ServerPort;
 34                 }
 35 
 36                 if (String.IsNullOrEmpty(FromAddress))
 37                 {
 38                     return "發件人不能爲空";
 39                 }
 40 
 41                 if (string.IsNullOrEmpty(FromAddressName))
 42                 {
 43                     mMailMessage.From = new MailAddress(FromAddress);
 44                 }
 45                 else
 46                 {
 47                     mMailMessage.From = new MailAddress(FromAddress, FromAddressName);
 48                 }
 49 
 50                 if (string.IsNullOrEmpty(Subject))
 51                 {
 52                     return "主旨不能爲空!";
 53                 }
 54 
 55                 if (ToAddressList == null || ToAddressList.Count() == 0)
 56                 {
 57                     return "收件人不能爲空!";
 58                 }
 59 
 60                 // mSmtpClient.UseDefaultCredentials = false;
 61                 mSmtpClient.Credentials = new NetworkCredential(UserName, Password); //設置發送人的郵箱帳號和密碼,163郵箱有設置受權碼
 62                 mSmtpClient.EnableSsl = true;  //啓用ssl,也就是安全發送
 63 
 64                 foreach (var item in ToAddressList)  //收件者
 65                 {
 66                     mMailMessage.To.Add(item);
 67                 }
 68                 if (CCAddressList != null && CCAddressList.Count() != 0)
 69                 {
 70                     foreach (var item in CCAddressList)  //副本
 71                     {
 72                         mMailMessage.CC.Add(item);
 73                     }
 74                 }
 75 
 76                 if (BccAddressList != null && BccAddressList.Count() != 0)
 77                 {
 78                     foreach (var item in BccAddressList)  //密件抄送人員
 79                     {
 80                         mMailMessage.Bcc.Add(item);
 81                     }
 82                 }
 83 
 84                 mMailMessage.Subject = Subject;
 85                 mMailMessage.IsBodyHtml = IsHtml; //正文是否使用html標籤展現
 86                 mMailMessage.Body = Body; //正文
 87                 mMailMessage.BodyEncoding = System.Text.Encoding.UTF8; //正文的編碼原則
 88                 mMailMessage.Priority = MailPriority; //優先級
 89 
 90                 if (AttachMentFileList != null && AttachMentFileList.Count() != 0)  //判斷是否有附件
 91                 {
 92                     foreach (var item in AttachMentFileList)
 93                     {
 94                         Attachment mAttachment = new Attachment(item.FilePath); //添加文件的文職
 95                         mAttachment.Name = item.FileName;//附件顯示的文件名稱
 96                         mAttachment.NameEncoding = System.Text.Encoding.UTF8;
 97                         mAttachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
 98                         mAttachment.ContentDisposition.Inline = true;
 99                         mAttachment.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment;
100                         mMailMessage.Attachments.Add(mAttachment);
101                     }
102                 }
103 
104                 mSmtpClient.Send(mMailMessage);
105                 return "發送成功!";
106 
107             }
108             catch (Exception ex)
109             {
110                 return ex.Message;
111             }
112         }
113 
114         public string SendMail(String ServerIP, string UserName, string Password, string FromAddress, string FromAddressName, string Subject, string Body, List<string> ToAddressList)
115         {
116             return SendMail(ServerIP, null, UserName, Password, FromAddress, FromAddressName, Subject, Body, ToAddressList, null, null, false, null, MailPriority.Normal);
117         }
118     }
119     public  class AttachmentFile
120     {
121         /// <summary>
122         /// 文件路徑
123         /// </summary>
124         public string FilePath { get; set; }
125         /// <summary>
126         ///附件顯示的文件明
127         /// </summary>
128         public string FileName { get; set; }
129     }

上面的代碼能夠直接使用。基本的方法都有,在這兒須要說明下AttachmentFile類是我用於發送文件的時候能夠從新命名文件名。服務器

注意:你們在使用的時候注意一下參數UserName和Password,在這兒我以我163的郵箱爲例,解釋下這兩個參數的做用。編碼

 

 在使用163郵箱服務器發送信息時須要按照圖片上的操做開啓客戶端受權密碼,上面的UserName和Password就是咱們的郵箱地址和受權碼。至於QQ郵箱我有試驗過,可是就是沒法發送郵件,至於大家說的開啓POP3和SMTP的選項,我也已經開啓了,若是知道一些緣由的話,能夠在下面留言告訴我!感激!spa

但願能幫到你們!code

相關文章
相關標籤/搜索