C#發(fā)送郵箱實現(xiàn)代碼
更新時間:2017年01月03日 14:50:48 作者:獨釣寒江雪丶
這篇文章主要為大家詳細介紹了C#發(fā)送郵箱實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
之前自己從來沒有做過發(fā)送郵箱的功能,前段時間項目需要,在找了很多帖子之后,終于實現(xiàn)了。
之后有整理了一下,寫了一個類。直接給類傳遞信息,就可以發(fā)送了。
這里還需要說明的是,發(fā)送郵箱需要開通POP3/SMTP服務(wù),否則QQ郵箱,網(wǎng)易郵箱等會報錯。接收的郵箱就不用開通啦,開通方法百度一下就知道啦。
public static class EmailHelper { /// <summary> /// 發(fā)送郵件 /// </summary> /// <param name="subject">郵件主題</param> /// <param name="msg">郵件內(nèi)容</param> /// <param name="filePath">附件地址,如果不添加附件傳null或""</param> /// <param name="senderEmail">發(fā)送人郵箱地址</param> /// <param name="senderPwd">發(fā)送人郵箱密碼</param> /// <param name="recipientEmail">接收人郵箱</param> public static void SendMail(string subject, string msg, string filePath, string senderEmail, string senderPwd, params string[] recipientEmail) { if (!CheckIsNotEmptyOrNull(subject, msg, senderEmail, senderPwd) || recipientEmail == null || recipientEmail.Length == 0) { throw new Exception("輸入信息無效"); } try { string[] sendFromUser = senderEmail.Split('@'); //構(gòu)造一個Email的Message對象 MailMessage message = new MailMessage(); //確定smtp服務(wù)器地址。實例化一個Smtp客戶端 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp." + sendFromUser[1]); //構(gòu)造發(fā)件人地址對象 message.From = new MailAddress(senderEmail, sendFromUser[0], Encoding.UTF8); //構(gòu)造收件人地址對象 foreach (string userName in recipientEmail) { message.To.Add(new MailAddress(userName, userName.Split('@')[0], Encoding.UTF8)); } if (!string.IsNullOrEmpty(filePath)) { Attachment attach = new Attachment(filePath); //得到文件的信息 ContentDisposition disposition = attach.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(filePath); disposition.ModificationDate = System.IO.File.GetLastWriteTime(filePath); disposition.ReadDate = System.IO.File.GetLastAccessTime(filePath); //向郵件添加附件 message.Attachments.Add(attach); } //添加郵件主題和內(nèi)容 message.Subject = subject; message.SubjectEncoding = Encoding.UTF8; message.Body = msg; message.BodyEncoding = Encoding.UTF8; //設(shè)置郵件的信息 client.DeliveryMethod = SmtpDeliveryMethod.Network; message.BodyEncoding = System.Text.Encoding.UTF8; message.IsBodyHtml = false; //如果服務(wù)器支持安全連接,則將安全連接設(shè)為true。 //gmail,qq支持,163不支持 switch (sendFromUser[1]) { case "gmail.com": case "qq.com": client.EnableSsl = true; break; default: client.EnableSsl = false; break; } //設(shè)置用戶名和密碼。 client.UseDefaultCredentials = false; //用戶登陸信息 NetworkCredential myCredentials = new NetworkCredential(senderEmail, senderPwd); client.Credentials = myCredentials; //發(fā)送郵件 client.Send(message); } catch (Exception ex) { throw (ex); } } /// <summary> /// 驗證所有傳入字符串不能為空或null /// </summary> /// <param name="ps">參數(shù)列表</param> /// <returns>都不為空或null返回true,否則返回false</returns> public static bool CheckIsNotEmptyOrNull(params string[] ps) { if (ps != null) { foreach (string item in ps) { if (string.IsNullOrEmpty(item)) return false; } return true; } return false; } }
直接調(diào)用方法,傳遞需要發(fā)送的信息,就可以發(fā)送郵箱了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
漢字轉(zhuǎn)拼音軟件制件示例(漢字轉(zhuǎn)字母)
這篇文章主要介紹了c#漢字轉(zhuǎn)拼音的方法,但不能判斷多音字,大家可以參考修改使用2014-01-01