No cow b design pattern, code refining and practical, full-featured, simple call. All finished for the yards farming considerations
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");//Dark send can be multiple Ms. Setishtml (TRUE);//default: True Ms. Setencoding (System.Text.Encoding.UTF8);//format default Utf-8 Ms. Setisssl (TRUE);//Whether SSL encryption defaults to FALSE //Call 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//* * Initiated: 2015-6-8//* * Modified:-//* * Author: Sunkaixuan///</summary> public class Mailsmtp {// /<summary>//Set message encoding type///</summary>//<param name= "contentencoding" ></param> Pub LIC void setencoding (Encoding contentencoding) {this._encoding = contentencoding; }///<summary>//Set whether the message body 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 Set CC (params string[] cc) {this._cc = cc; }///<summary>////</summary>///Lt;param name= "CC" ></param> public void SETBC (params string[] BC) {this._bcc = BC; }//<summary>//Whether SSL encryption///</summary>//<param name= "Isssl" ></param> public void Setisssl (bool isssl) {this._smtp. Enablessl = Isssl; }///<summary>//constructor///</summary>//<param name= "host" ></param>///<par Am Name= "username" > Email account </param>//<param name= "password" > Password </param> public mailsmtp (String ho St, 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 = false; } else {this._smtp. Credentials = new NetworkCredential (username, password); }}///<summarY>////</summary>//<param name= "from" > Sender e-mail address </param>//<param name= "Sen Der "> Sender display Name </param>//<param name=" to "> Recipient address </param>///<param Name=" Subject "> Message header < ;/param>//<param name= "body" > Message body </param>//<param name= "file" > Attachment address array </param>// <returns>bool Success </returns> public bool Send (string from, string sender, String to, string subject, Stri ng body, params string[] file) {return Send (from, sender, new string[] {to}, subject, body, file); }///<summary>//E-mail///</summary>//<param name= "from" > sender's email address </param>// <param name= "Sender" > Sender display Name </param>//<param name= "to" > Recipient address </param>//<param name= " Subject "> Mail title </param>//<param name=" body "> Message body </param>///<param name=" file "> Attachment address array &l t;/param>//<RETURNS>bool Success </returns> public bool Send (string from, string sender, string[) to, string subject, string body, par AMS string[] file) {string mailreg = @ "^[\w-]+ (\.[ \w-]+) *@[\w-]+ (\.[ \w-]+) +$ "; if (to = = null) {throw new ArgumentNullException ("MailSmtp.Send.to"); } if (to. Any (OIT =!) Regex.IsMatch (Oit + "", Mailreg))) {this. Result = "The recipient address is not valid"; return false; } if (_BCC! = null && _bcc. Length > 0) {if (_bcc. Any (OIT =!) Regex.IsMatch (Oit + "", Mailreg))) {this. Result = "The dark send person address is illegal"; return false; }} if (_cc! = null && _cc. Length > 0) {if (_cc. Any (OIT =!) Regex.IsMatch (Oit + "", Mailreg))) {this. Result = "CC person address is not valid"; return false; }} MailMessage message = new MailMessage (); Create an Attachment object, foreach (var r in file) {Attachment objmailattachment; ObjmailAttachment = new Attachment (r);//The attachment message that sent the message. Attachments.Add (objmailattachment); } message. from = new MailAddress (from, sender); Message. Subject = Subject; Message. subjectencoding = this._encoding; Message. 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>///Get send result, success is NULL//</summary> public string result {get; private set;} } }
The above mentioned is the whole content of this article, I hope you can like.