Mail sending class ASP. net c #,
Without the design pattern of niub, the code is refined, practical, complete, and simple to call .. Full completion for coders
MailSmtp ms = new MailSmtp ("smtp.qq.com", "1215247044", "xxxx"); // optional parameter ms. setCC ("610262374@qq.com"); // CC can be multiple ms. setBC ("610262374@qq.com"); // BCC can be multiple ms. setIsHtml (true); // default value: true ms. setEncoding (System. text. encoding. UTF8); // The default format is UTF-8 ms. setIsSSL (true); // whether ssl encryption is set to false by default. // call the function bool isSuccess = ms. send ("1215247044@qq.com", "test", "610262374@qq.com", "Haha", "Haha", Server. mapPath ("~ /Test. dll "); // output Result Response. Write (ms. Result );
Code:
Using System; using System. IO; using System. web. UI. webControls; using System. text; using System. net. mail; using System. net; using System. linq; using System. text. regularExpressions; namespace SyntacticSugar {// <summary> // ** Description: email sending // ** Creation Time: // ** modification time: -/// ** Author: sunkaixuan // ** usage instructions: http://www.cnblogs.com/sunkaixuan/p/4562147.html /// </Summary> public class MailSmtp {// <summary> /// set the mail encoding type /// </summary> /// <param name = "contentEncoding "> </param> public void SetEncoding (Encoding contentEncoding) {this. _ encoding = contentEncoding ;} /// <summary> /// set whether the email body is in Html format. /// </summary> /// <param name = "isHtml"> </param> public void setIsHtml (bool isHtml) {this. _ isHtml = isHtml;} // <summary> // CC // </summary> // <pa Ram name = "cc"> </param> public void SetCC (params string [] cc) {this. _ cc = cc ;} /// <summary> /// send a message in the dark /// </summary> /// <param name = "cc"> </param> public void SetBC (params string [] bc) {this. _ bcc = bc ;} /// <summary> /// whether to enable ssl encryption /// </summary> /// <param name = "isSSL"> </param> public void SetIsSSL (bool isSSL) {this. _ smtp. enableSsl = isSSL;} // <summary> // constructor // </summary> /// <param name =" Host "> </param> /// <param name =" username "> email account </param> /// <param name =" password "> password </param> public MailSmtp (string host, string username, string password) {this. _ smtp. host = host; this. _ smtp. port = 0x19; this. _ smtp. enableSsl = false; this. _ isHtml = true; this. _ encoding = Encoding. UTF8; if (string. isNullOrEmpty (username) | string. isNullOrEmpty (password) {this. _ smtp. useDefaultCredentials = fals E;} else {this. _ smtp. credentials = new NetworkCredential (username, password );}} /// <summary> /// send email /// </summary> /// <param name = "from"> sender email address </param> /// <param name = "sender"> sender display name </param> /// <param name = "to"> recipient address </param> /// <param name = "subject "> email title </param> /// <param name =" body "> email body </param> /// <param name =" file "> array of attachment addresses </param> // <returns> whether bool is successful </returns> public bool Send (String from, string sender, string to, string subject, string body, params string [] file) {return Send (from, sender, new string [] {to}, subject, body, file );} /// <summary> /// send email /// </summary> /// <param name = "from"> sender email address </param> /// <param name = "sender"> sender display name </param> /// <param name = "to"> recipient address </param> /// <param name = "subject "> email title </param> /// <param name =" body "> email body </param> /// <par Am name = "file"> attachment address array </param> // <returns> whether bool is successful </returns> public bool Send (string from, string sender, string [] to, string subject, string body, params string [] file) {string mailReg = @ "^ [\ w-] + (\. [\ w-] +) * @ [\ w-] + (\. [\ w-] +) + $ "; if (to = null) {throw new ArgumentNullException (" MailSmtp. send. to ");} if (. any (oit =>! Regex. IsMatch (oit + "", mailReg) {this. Result = "invalid recipient address"; return false ;}if (_ bcc! = Null & _ bcc. Length> 0) {if (_ bcc. Any (oit =>! Regex. IsMatch (oit + "", mailReg) {this. Result = "The Hidden recipient address is invalid"; return false ;}} if (_ cc! = Null & _ cc. Length> 0) {if (_ cc. Any (oit =>! Regex. isMatch (oit + "", mailReg) {this. result = "invalid CC address"; return false ;}} MailMessage message = new MailMessage (); // create an Attachment object foreach (var r in file) {Attachment objMailAttachment; objMailAttachment = new Attachment (r); // The Attachment message of the email. attachments. add (objMailAttachment);} message. from = new MailAddress (from, sender); message. subject = subject; message. subjectEncoding = this. _ encoding; me Ssage. body = body; message. bodyEncoding = this. _ encoding; message. isBodyHtml = this. _ isHtml; message. priority = MailPriority. normal; foreach (string str in to) {message. to. add (str);} if (this. _ bcc! = Null & this. _ bcc. length> 0) {foreach (string B in this. _ bcc) {message. bcc. add (B) ;}} if (this. _ cc! = Null & this. _ cc. length> 0) {foreach (string c in this. _ cc) {message. CC. add (c) ;}try {this. _ smtp. send (message); return true;} catch (Exception ex) {Console. writeLine (ex. message);} return false;} private SmtpClient _ smtp = new SmtpClient (); private Encoding _ encoding {get; set;} private bool _ isHtml {get; set ;} private string [] _ cc {get; set;} private string [] _ bcc {get; set ;}/// <summary> /// obtain the sending result, otherwise, it is null. // </summary> public string Result {get; private set ;}}}