Mailbox send finally solved today, never to the meeting with 3 nights to finally solve, there are many problems are not code problems, but the mailbox settings on the problem. Let me explain in one by one below.
1. The principle of email sending, I use the picture to explain
On the left [email protected] is sent to the mailbox (I am using a mailbox to refer to), the right [email protected] is to receive the mailbox (I am using B mailbox refers to).
1), mailbox A is sent to his own SMTP server, such as Mailbox A is a mailbox registered on Outlook, then mail for mailbox A is sent to the SMTP server on Outlook
2), through the communication rules of the SMTP server, to the SMTP server to Mailbox B, an SMTP server is sent to the storage device, and then sent to the POP3 server, and finally sent to mailbox B
Note: The most important is 1), here the main explanation to send.
2. The following is the class that sends the message
usingSystem;usingSystem.Linq;usingSystem.Net.Mail;usingSystem.Text;namespacemicua.infrastructure.utility{/// <summary> ///Mail Send helper class/// </summary> /// <remarks> ///2013-11-18 18:56 Created by IceStone/// </remarks> Public Static classMailhelper {Private ReadOnly Static stringSmtpServer ="server address for SMTP";//smtp.wedn.net Private ReadOnly Static intSmtpserverport = -; Private ReadOnly Static BOOLSmtpenablessl =false; Private ReadOnly Static stringSmtpusername ="sent Mailbox"; Private ReadOnly Static stringSmtpdisplayname ="test Mailbox 123"; Private ReadOnly Static stringSmtppassword ="location of the authorization code"; /// <summary> ///send a message to a specified recipient/// </summary> /// <remarks> ///2013-11-18 18:55 Created by IceStone/// </remarks> /// <param name= "to" >Recipient Address</param> /// <param name= "Subject" >Theme</param> /// <param name= "Mailbody" >body content (HTML supported)</param> /// <param name= "Copytos" >cc Address List</param> /// <returns>whether the send was successful</returns> Public Static BOOLSend (stringTo,stringSubjectstringMailbody,params string[] copytos) { returnSend (New[] {to}, subject, Mailbody, Copytos,New string[] {}, mailpriority.normal); } /// <summary> ///send a message to a specified recipient/// </summary> /// <remarks> ///2013-11-18 18:55 Created by IceStone/// </remarks> /// <param name= "tos" >Recipient Address List</param> /// <param name= "Subject" >Theme</param> /// <param name= "Mailbody" >body content (HTML supported)</param> /// <param name= "CCS" >cc Address List</param> /// <param name= "BCCs" >Blind carbon copy address list</param> /// <param name= "priority" >priority of this message</param> /// <param name= "Attachments" >List of attachments</param> /// <returns>whether the send was successful</returns> /// <exception cref= "System.ArgumentNullException" >Attachments</exception> Public Static BOOLSend (string[] tos,stringSubjectstringMailbody,string[] CCS,string[] BCCs, mailpriority priority,paramsattachment[] Attachments) { if(Attachments = =NULL)Throw NewArgumentNullException ("Attachments"); if(TOS. Length = =0)return false; //Create an email entity varMessage =NewMailMessage (); Message. from=Newmailaddress (Smtpusername, smtpdisplayname); Message. Subject=subject; Message. Body=Mailbody; Message. Bodyencoding=Encoding.UTF8; Message. Isbodyhtml=true; Message. Priority=Priority ; //Insert Attachment foreach(varAttachmentinchattachments) {message.} Attachments.Add (attachment); } //Insert recipient address, CC address, and BCC address foreach(varToinchTos. Where (c =!)string. IsNullOrEmpty (c))) {message. To.add (Newmailaddress (to)); } foreach(varCcinchCcs. Where (c =!)string. IsNullOrEmpty (c))) {message. Cc. ADD (Newmailaddress (cc)); } foreach(varBccinchBCCs. Where (c =!)string. IsNullOrEmpty (c))) {message. Cc. ADD (Newmailaddress (BCC)); } //Create an SMTP client varClient =NewSmtpClient {Host=SmtpServer, Credentials=NewSystem.Net.NetworkCredential (Smtpusername, Smtppassword), Deliverymethod=Smtpdeliverymethod.network, Enablessl=Smtpenablessl, Port=Smtpserverport}; //client. sendcompleted + = client_sendcompleted; //Try//{ //Send mailclient. Send (message); //client. SendAsync (Message,datetime.now.tostring ()); //client. Dispose (); //message. Dispose (); return true; //} //catch (Exception)//{ //throw; //} } }}
The main is to change the front of a few private static variables, the following I carefully explain, I use NetEase mailbox as an example, I casually make a mailbox ([email protected], Authorization code: CESHI123)
Fill in the SMTP address, such as: NetEase's SMTP server address is smtp.163.comprivate readonly static int smtpserverport =;
This is not changed, this is the port number private readonly static bool Smtpenablessl = False;
This does not have to change, if set to true, the above port number will change, change to be like 465, this I am not sure private readonly static string smtpusername = "Sent Mailbox";
Send the mailbox, such as: NetEase's mailbox [email protected]private readonly static string smtpdisplayname = "Test mailbox 123";
This place itself can be arbitrarily set up to see what effect the private readonly static string Smtppassword = "Location of the Authorization Code";
This is the most important, I finally spent a lot of time on this, the authorization code is a third-party client login needs to input, the first need to set up in the mailbox, this and the code does not have any relationship, the following I focus on.
Finally, this class is called.
3. How to have the authorization code of the mailbox
such as NetEase e-mail: You can Baidu, you can also follow my general ideas to set up, different mailboxes have different settings, basically is the login Web mailbox, click Settings, find out about the SMTP service settings, you can.
Note: QQ mailbox I set up, but no, I use is netease can, do not know other line not, we can try, what do not know can give me message, if you know how to set up QQ mailbox, you can give me to say, let me also learn, thank you. I hope this article will be of some help to you.
e-mail messages in ASP.