There is no Niu B's design pattern, the code is concise and practical, complete functions, and simple to call. . All for the coder to consider
MailSmtp ms = new MailSmtp("","1215247044","xxxx"); //Optional parameters ("610262374@");//Cross can be multiple ("610262374@");//Secret delivery can be multiple (true);//Default: true (.UTF8);//Set format default utf-8 (true);//Is SSL encryption default to false //Calling the function bool isSuccess = ("1215247044@", "test", "610262374@", "Ha ha", "Ha ha", ("~/")); //Output result ();
Code:
using System; using ; using ; using ; using ; using ; using ; using ; namespace SyntacticSugar { /// <summary> /// ** Description: Email sent /// ** Founding date: 2015-6-8 /// ** Modification time:- /// ** Author: sunkaixuan /// </summary> public class MailSmtp { /// <summary> /// Set the email code type /// </summary> /// <param name="contentEncoding"></param> public void SetEncoding(Encoding contentEncoding) { this._encoding = contentEncoding; } /// <summary> ///Set whether the email text is in Html format /// </summary> /// <param name="isHtml"></param> public void SetIsHtml(bool isHtml) { this._isHtml = isHtml; } /// <summary> /// Cc /// </summary> /// <param name="cc"></param> public void SetCC(params string[] cc) { this._cc = cc; } /// <summary> /// Secret delivery /// </summary> /// <param name="cc"></param> public void SetBC(params string[] bc) { this._bcc = bc; } /// <summary> /// Is 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 ((username) || (password)) { this._smtp.UseDefaultCredentials = false; } 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 text</param> /// <param name="file">Attachment address array</param> /// <returns>Whether the bool succeed </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 text</param> /// <param name="file">Attachment address array</param> /// <returns>Whether the bool succeed </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(""); } if ((oit => !(oit + "", mailReg))) { = "The recipient's address is illegal"; return false; } if (_bcc != null && _bcc.Length > 0) { if (_bcc.Any(oit => !(oit + "", mailReg))) { = "Improper address for secret delivery"; return false; } } if (_cc != null && _cc.Length > 0) { if (_cc.Any(oit => !(oit + "", mailReg))) { = "The address of the cc sender is illegal"; return false; } } MailMessage message = new MailMessage(); // Create an attachment object foreach (var r in file) { Attachment objMailAttachment; objMailAttachment = new Attachment(r);//Attachment of sending email (objMailAttachment); } = new MailAddress(from, sender); = subject; = this._encoding; = body; = this._encoding; = this._isHtml; = ; foreach (string str in to) { (str); } if (this._bcc != null && this._bcc.Length > 0) { foreach (string b in this._bcc) { (b); } } if (this._cc != null && this._cc.Length > 0) { foreach (string c in this._cc) { (c); } } try { this._smtp.Send(message); return true; } catch (Exception ex) { (); } 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> /// Get the send result, if successful, it will be empty /// </summary> public string Result { get; private set; } } }
The above is the entire content of this article, I hope you like it.