C#發送郵箱。本站提示廣大學習愛好者:(C#發送郵箱)文章只能為提供參考,不一定能成為您想要的結果。以下是C#發送郵箱正文
之前自己歷來沒有做過發送郵箱的功用,前段時間項目需求,在找了很多帖子之後,終於完成了。
之後有整理了一下,寫了一個類。直接給類傳遞信息,就可以發送了。
這裡還需求闡明的是,發送郵箱需求守舊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 }
,直接調用辦法,傳遞需求發送的信息,就可以發送郵箱了。