C # mailbox Mail Send Class

Source: Internet
Author: User

The functionality of this class includes sending the message, the correct mailbox format, and determining the correct mailbox username and password without sending the message, and resolving the problem with an asynchronous thread, given that the pop checks the mailbox user name and password for the delay in returning the result of the error, see Code

No cow b design mode, code concise and practical, full-featured, simple to invoke. All finished for the yard farmers consider

?

1 2 3 4 5 6 7 8 9 10 11-12 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);//Set 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 Results Response.Write (Ms. result);

Code:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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: Mail///* * Founder Time: 2015-6-8///* * Modified:-///* * Author: Sunkaixuan/// </summary> public class Mailsmtp {///<summary>///setting message encoding type///</summary>///<param name= "content Encoding "></param> public void setencoding (Encoding contentencoding) {this._encoding = contentencoding;}///& Lt;summary>///to set whether the message body is Html///</summary>///<param name= "ishtml" ></param> public void Setish TML (bool ishtml) {this._ishtml = ishtml;}///<summary>///cc///</summary>///<param name= "CC" >&lt ;/param> public void SETCC (params string[] cc) {this._cc = cc;///<summary>///Dark send///</summary>///& Lt;param name= "CC" ></param> public void SETBC (params string[] BC) {THIS._BCC = bc }///<summary>///SSL encryption///</summary>///<param name= "Isssl" ></param> public void Setisssl (b Ool Isssl) {this._smtp. Enablessl = Isssl; ///<summary>///Constructor///</summary>///<param name= "host" ></param>///<param name= "Userna" Me > Mail 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 = false; else {this._smtp. Credentials = new NetworkCredential (username, password); }///<summary>///send mail///</summary>///<param name= "from" > Sender email address </param>///<param nam E= "Sender" > Sender display name </param>///<param name= "to" > Recipient address </param>///<param name= "Subject" > Message Title </param>///<param name= "Body" > Message text </param>///<param name= "file" > Attachment address array </param>///< Returns>bool Success </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 Mail///</summary>///<param name= "from" > Senders mail address </param>///<param name= "Sender" > Sender display Name </ param>///<param name= "to" > Recipient address </param>///<param name= "subject" > Message title </param>///< param name= "Body" > Message text </param>///<param name= "file" > Attachment address array </param>///<returns>bool Success </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 (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 = "Dark Sender address is not lawful"; return false; } if (_cc!= null && _cc. Length > 0) {if (_CC). Any (OIT =>!) Regex.IsMatch (Oit + "", Mailreg))) {this. result = "CC address is illegal"; return false; } mailmessage message = new MailMessage (); Create an Attachment object foreach (var r in file) {attachment objmailattachment; objmailattachment = new Attachment (r);//Send message attachment Messag E.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; The 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 str Ing[] _BCC {get; set;}///<summary>///gets the send result, the success is null///</summary> public string Results {got; private set ; }}  }

The above is the entire contents of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.