C#簡單郵件群發(fā)通用類
本文實(shí)例為大家介紹了C#郵件群發(fā)通用類,供大家參考,具體內(nèi)容如下
public static class Email { /// <summary> /// 發(fā)件人 /// </summary> public static string mailFrom { get; set; } /// <summary> /// 收件人 /// </summary> public static string[] mailToArray { get; set; } /// <summary> /// 抄送 /// </summary> public static string[] mailCcArray { get; set; } /// <summary> /// 標(biāo)題 /// </summary> public static string mailSubject { get; set; } /// <summary> /// 正文 /// </summary> public static string mailBody { get; set; } /// <summary> /// 發(fā)件人密碼 /// </summary> public static string mailPwd { get; set; } /// <summary> /// SMTP郵件服務(wù)器 /// </summary> public static string host { get; set; } /// <summary> /// 郵件服務(wù)器端口 /// </summary> public static int port { get; set; } /// <summary> /// 正文是否是html格式 /// </summary> public static bool isbodyHtml { get; set; } /// <summary> /// 附件 /// </summary> public static string[] attachmentsPath { get; set; } public static bool Send() { //使用指定的郵件地址初始化MailAddress實(shí)例 MailAddress maddr = new MailAddress(mailFrom); //初始化MailMessage實(shí)例 MailMessage myMail = new MailMessage(); //向收件人地址集合添加郵件地址 if (mailToArray != null) { for (int i = 0; i < mailToArray.Length; i++) { myMail.To.Add(mailToArray[i].ToString()); } } //向抄送收件人地址集合添加郵件地址 if (mailCcArray != null) { for (int i = 0; i < mailCcArray.Length; i++) { myMail.CC.Add(mailCcArray[i].ToString()); } } //發(fā)件人地址 myMail.From = maddr; //電子郵件的標(biāo)題 myMail.Subject = mailSubject; //電子郵件的主題內(nèi)容使用的編碼 myMail.SubjectEncoding = Encoding.UTF8; //電子郵件正文 myMail.Body = mailBody; //電子郵件正文的編碼 myMail.BodyEncoding = Encoding.Default; //電子郵件優(yōu)先級(jí) myMail.Priority = MailPriority.High; //電子郵件不是html格式 myMail.IsBodyHtml = isbodyHtml; //在有附件的情況下添加附件 try { if (attachmentsPath != null && attachmentsPath.Length > 0) { Attachment attachFile = null; foreach (string path in attachmentsPath) { attachFile = new Attachment(path); myMail.Attachments.Add(attachFile); } } } catch (Exception err) { throw new Exception("在添加附件時(shí)有錯(cuò)誤:" + err.Message); } SmtpClient client = new SmtpClient(); //指定發(fā)件人的郵件地址和密碼以驗(yàn)證發(fā)件人身份 client.Credentials = new NetworkCredential(mailFrom, mailPwd); //設(shè)置SMTP郵件服務(wù)器 //client.Host = "smtp." + myMail.From.Host; client.Host = host; //SMTP郵件服務(wù)器端口 client.Port = port; //是否使用安全連接 //client.EnableSsl = true; try { //將郵件發(fā)送到SMTP郵件服務(wù)器 client.Send(myMail); return true; } catch (SmtpException ex) { string msg = ex.Message; return false; } }
希望本文所述對(duì)大家學(xué)習(xí)C#程序設(shè)計(jì)有所幫助。
- C#編程中枚舉類型的使用教程
- 結(jié)合.net框架在C#派生類中觸發(fā)基類事件及實(shí)現(xiàn)接口事件
- C#使用LINQ中Enumerable類方法的延遲與立即執(zhí)行的控制
- C#中的委托數(shù)據(jù)類型簡介
- ASP.NET在底層類庫中獲取Session C#類中獲取Session
- C#中的靜態(tài)成員、靜態(tài)方法、靜態(tài)類介紹
- C#使用Process類調(diào)用外部exe程序
- C#中使用XmlDocument類來創(chuàng)建和修改XML格式的數(shù)據(jù)文件
- C#實(shí)現(xiàn)動(dòng)態(tài)生成靜態(tài)頁面的類詳解
- 分享C#中幾個(gè)可用的類
相關(guān)文章
C#連接到sql server2008數(shù)據(jù)庫的實(shí)例代碼
這篇文章主要介紹了C#連接到sql server2008數(shù)據(jù)庫的實(shí)例代碼,需要的朋友可以參考下2017-09-09c#中利用委托反射將DataTable轉(zhuǎn)換為實(shí)體集的代碼
c#中利用委托反射將DataTable轉(zhuǎn)換為實(shí)體集的代碼,需要的朋友可以參考下2012-10-10C#實(shí)現(xiàn)保存文件時(shí)重名自動(dòng)生成新文件的方法
這篇文章主要介紹了C#實(shí)現(xiàn)保存文件時(shí)重名自動(dòng)生成新文件的方法,涉及C#針對(duì)保存文件時(shí)出現(xiàn)重命名情況的自動(dòng)處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法
這篇文章主要介紹了C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法,以一個(gè)完整的猜拳游戲?yàn)槔v述了C#面向?qū)ο蟪绦蛟O(shè)計(jì)的具體實(shí)現(xiàn)步驟,具有一定的學(xué)習(xí)與借鑒價(jià)值,需要的朋友可以參考下2014-11-11C#中Json字符串的各種應(yīng)用類實(shí)例講解
這篇文章主要介紹了C#中Json字符串的各種應(yīng)用類實(shí)例講解的相關(guān)資料,需要的朋友可以參考下2015-10-10C#實(shí)現(xiàn)發(fā)送手機(jī)驗(yàn)證碼功能
之前基于c#實(shí)現(xiàn)手機(jī)發(fā)送驗(yàn)證碼功能很復(fù)雜,真正做起來也就那回事,不過就是一個(gè)post請(qǐng)求就可以實(shí)現(xiàn)的東西,今天小編把思路分享到腳本之家平臺(tái),供大家參考下2017-06-06