之前自己從來(lái)沒(méi)有做過(guò)發(fā)送郵箱的功能,前段時(shí)間項(xiàng)目需要,在找了很多帖子之后,終于實(shí)現(xiàn)了。
之后有整理了一下,寫(xiě)了一個(gè)類(lèi)。直接給類(lèi)傳遞信息,就可以發(fā)送了。
這里還需要說(shuō)明的是,發(fā)送郵箱需要開(kāi)通POP3/SMTP服務(wù),否則QQ郵箱,網(wǎng)易郵箱等會(huì)報(bào)錯(cuò)。接收的郵箱就不用開(kāi)通啦,開(kāi)通方法百度一下就知道啦。
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("輸入信息無(wú)效"); } try { string[] sendFromUser = senderEmail.Split('@'); //構(gòu)造一個(gè)Email的Message對(duì)象 MailMessage message = new MailMessage(); //確定smtp服務(wù)器地址。實(shí)例化一個(gè)Smtp客戶端 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp." + sendFromUser[1]); //構(gòu)造發(fā)件人地址對(duì)象 message.From = new MailAddress(senderEmail, sendFromUser[0], Encoding.UTF8); //構(gòu)造收件人地址對(duì)象 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> /// 驗(yàn)證所有傳入字符串不能為空或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ā)送郵箱了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注