Send email in asp.net and asp.net

Source: Internet
Author: User

Send email in asp.net and asp.net

The mailbox was finally resolved today, and it never took three nights to finally solve the problem. Many problems are not code problems, but mailbox settings problems. I will explain it one by one.

1. How email sending works. I use images to explain it.

The user_a@itcast.cn on the left is the mailbox to be sent (I am referring to a mailbox below), the user_ B @itheima.com on the right is the mailbox to receive (below I am referring to B mailbox ).

1) email a is sent to his smtp server. For example, if email a is a registered mailbox on outlook, then email a is sent to the smtp server on outlook.

2) The communication rules of the smtp server are sent to the smtp server of mailbox B, the smtp server is sent to the storage device, then to the pop3 server, and finally to the mailbox B.

Note: the most important thing is 1). Here we will mainly explain sending.

2. The following is the mail sending class

Using System; using System. linq; using System. net. mail; using System. text; namespace Micua. infrastructure. utility {// <summary> /// email sending Assistant class /// </summary> /// <remarks> /// Created By iceStone /// </remarks> public static class MailHelper {private readonly static string SmtpServer = "smtp server address "; // smtp.wedn.net private readonly static int SmtpServerPort = 25; private readonly static Bool SmtpEnableSsl = false; private readonly static string SmtpUsername = "sent Email Address"; private readonly static string SmtpDisplayName = "test email address 123 "; private readonly static string SmtpPassword = "authorization code location "; /// <summary> /// send the email to the specified recipient /// </summary> /// <remarks> /// Created By iceStone /// </ remarks> /// <param name = "to"> recipient address </param> /// <param name = "subject"> topic </param> /// <param nam E = "mailBody"> body content (HTML supported) </param> /// <param name = "copyTos"> CC address list </param> /// <returns> whether sent successfully </returns> public static bool Send (string, string subject, string mailBody, params string [] copyTos) {return Send (new [] {to}, subject, mailBody, copyTos, new string [] {}, MailPriority. normal );} /// <summary> /// send the email to the specified recipient /// </summary> /// <remarks> // Created By iceStone /// </Remarks> /// <param name = "tos"> recipient address list </param> /// <param name = "subject"> topic </param> /// <param name = "mailBody"> body content (HTML supported) </param> /// <param name = "ccs"> CC address list </param> /// <param name = "bccs"> BCC address list </param> /// <param name = "priority"> priority of the email </param> /// <param name = "attachments"> Attachment List </param> /// <returns> whether the message is sent successfully </returns> // <exception cref = "System. argumentNullException "> attachments </exception> Public static bool Send (string [] tos, string subject, string mailBody, string [] ccs, string [] bccs, MailPriority priority, params Attachment [] attachments) {if (attachments = null) throw new ArgumentNullException ("attachments"); if (tos. length = 0) return false; // create the Email entity var message = new MailMessage (); message. from = new MailAddress (SmtpUsername, SmtpDisplayName); message. subject = subject; Message. body = mailBody; message. bodyEncoding = Encoding. UTF8; message. isBodyHtml = true; message. priority = priority; // Insert the attachment foreach (var attachment in attachments) {message. attachments. add (attachment);} // Insert the recipient address, CC address, and BCC address foreach (var to in tos. where (c =>! String. IsNullOrEmpty (c) {message. To. Add (new MailAddress (to);} foreach (var cc in ccs. Where (c =>! String. IsNullOrEmpty (c) {message. CC. Add (new MailAddress (cc);} foreach (var bcc in bccs. Where (c =>! String. isNullOrEmpty (c) {message. CC. add (new MailAddress (bcc);} // create the SMTP client var client = new SmtpClient {Host = SmtpServer, Credentials = new System. net. networkCredential (SmtpUsername, SmtpPassword), DeliveryMethod = SmtpDeliveryMethod. network, EnableSsl = SmtpEnableSsl, Port = SmtpServerPort}; // client. sendCompleted + = Client_SendCompleted; // try // {// send the email client. send (message); // client. sendAsync (message, DateTime. now. toString (); // client. dispose (); // message. dispose (); return true; //} // catch (Exception) // {// throw ;//}}}}

The main is to change the previous several private static variables, I will explain carefully below, I take Netease mailbox as an example, just make a mailbox (ceshi@163.com, authorization code: ceshi123)

Private readonly static string SmtpServer = "smtp server address ";
Enter the smtp address. For example, the smtp server address of Netease is smtp.163.comprivate readonly static int SmtpServerPort = 25;
This does not need to be changed. This is the port number private readonly static bool SmtpEnableSsl = false;
This does not need to be changed. If it is set to true, the above port number should be changed to 465. I am not sure about private readonly static string SmtpUsername = "sent mailbox ";
Send mailbox, such as: Netease mailbox ceshi@163.comprivate readonly static string SmtpDisplayName = "test mailbox 123 ";
In this case, you can set it as needed to see what the effect is: private readonly static string SmtpPassword = "authorization code location ";
This is the most important thing. I spent a lot of time here. The authorization code is required for third-party client login. First, you need to set it in your mailbox, this has nothing to do with the code. I will explain it in detail below.

Finally, you can call this class.

3. How to have an email address authorization code

For example, Netease mail: You can use Baidu or set up according to my general idea. Different mailboxes have different settings. Basically, you can log on to the webpage mailbox and click Settings, find the settings for the smtp service.

Note: I can set QQ mail, but I cannot. I can use Netease. I don't know if I can do anything else. You can try it and leave a message if you don't know anything, if you know how to set up QQ mail, you can give me some information and let me learn it. Thank you. I hope this article will help you.

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.