之前自己從來沒有做過發送郵箱的功能,前段時間項目需要,在找了很多帖子之後,終於實現了。
之後有整理了一下,寫了一個類。直接給類傳遞信息,就可以發送了。
這裡還需要說明的是,發送郵箱需要開通POP3/SMTP服務,否則QQ郵箱,網易郵箱等會報錯。接收的郵箱就不用開通啦,開通方法百度一下就知道啦。
1 public static class EmailHelper 2 { 3 /// <summary> 4 /// 發送郵件 5 /// </summary> 6 /// <param name="subject">郵件主題</param> 7 /// <param name="msg">郵件內容</param> 8 /// <param name="filePath">附件地址,如果不添加附件傳null或""</param> 9 /// <param name="senderEmail">發送人郵箱地址</param> 10 /// <param name="senderPwd">發送人郵箱密碼</param> 11 /// <param name="recipientEmail">接收人郵箱</param> 12 public static void SendMail(string subject, string msg, string filePath, string senderEmail, string senderPwd, params string[] recipientEmail) 13 { 14 if (!CheckIsNotEmptyOrNull(subject, msg, senderEmail, senderPwd) || recipientEmail == null || recipientEmail.Length == 0) 15 { 16 throw new Exception("輸入信息無效"); 17 } 18 try 19 { 20 string[] sendFromUser = senderEmail.Split('@'); 21 22 //構造一個Email的Message對象 23 MailMessage message = new MailMessage(); 24 25 //確定smtp服務器地址。實例化一個Smtp客戶端 26 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp." + sendFromUser[1]); 27 28 //構造發件人地址對象 29 message.From = new MailAddress(senderEmail, sendFromUser[0], Encoding.UTF8); 30 31 //構造收件人地址對象 32 foreach (string userName in recipientEmail) 33 { 34 message.To.Add(new MailAddress(userName, userName.Split('@')[0], Encoding.UTF8)); 35 } 36 37 if (!string.IsNullOrEmpty(filePath)) 38 { 39 Attachment attach = new Attachment(filePath); 40 //得到文件的信息 41 ContentDisposition disposition = attach.ContentDisposition; 42 disposition.CreationDate = System.IO.File.GetCreationTime(filePath); 43 disposition.ModificationDate = System.IO.File.GetLastWriteTime(filePath); 44 disposition.ReadDate = System.IO.File.GetLastAccessTime(filePath); 45 //向郵件添加附件 46 message.Attachments.Add(attach); 47 } 48 49 //添加郵件主題和內容 50 message.Subject = subject; 51 message.SubjectEncoding = Encoding.UTF8; 52 message.Body = msg; 53 message.BodyEncoding = Encoding.UTF8; 54 55 //設置郵件的信息 56 client.DeliveryMethod = SmtpDeliveryMethod.Network; 57 message.BodyEncoding = System.Text.Encoding.UTF8; 58 message.IsBodyHtml = false; 59 60 //如果服務器支持安全連接,則將安全連接設為true。 61 //gmail,qq支持,163不支持 62 switch (sendFromUser[1]) 63 { 64 case "gmail.com": 65 case "qq.com": 66 client.EnableSsl = true; 67 break; 68 default: 69 client.EnableSsl = false; 70 break; 71 } 72 73 //設置用戶名和密碼。 74 client.UseDefaultCredentials = false; 75 //用戶登陸信息 76 NetworkCredential myCredentials = new NetworkCredential(senderEmail, senderPwd); 77 client.Credentials = myCredentials; 78 //發送郵件 79 client.Send(message); 80 } 81 catch (Exception ex) 82 { 83 throw (ex); 84 } 85 } 86 87 /// <summary> 88 /// 驗證所有傳入字符串不能為空或null 89 /// </summary> 90 /// <param name="ps">參數列表</param> 91 /// <returns>都不為空或null返回true,否則返回false</returns> 92 public static bool CheckIsNotEmptyOrNull(params string[] ps) 93 { 94 if (ps != null) 95 { 96 foreach (string item in ps) 97 { 98 if (string.IsNullOrEmpty(item)) return false; 99 } 100 return true; 101 } 102 return false; 103 } 104 }
,直接調用方法,傳遞需要發送的信息,就可以發送郵箱了。