Send email in ASP. NET 3.5

Source: Internet
Author: User

Method 1: cs code using System. net. mail; using System. net; string mailServerName = "smtp.qq.com"; // SMTP server for sending mail string mailFrom = "xxxxxx@qq.com"; // sender's mailbox (with the 126 mail server, you must use the username of the 126 mailbox) string mailTo = "xxxxxx@qq.com"; // recipient mailbox string subject = "send mail by code "; // mail subject string body = "here is the mail body"; // mail body using (MailMessage message = new MailMessage (mailFrom, mailTo, subject, body )) {// SmtpClient is the body of the sent email. This constructor notifies SmtpClie Which SMTP server SmtpClient mailClient = new SmtpClient (mailServerName) is used when nt sends an email? // assign the authentication instance to mailClient, that is, the user name and password used to access the SMTP server mailClient. credentials = new NetworkCredential ("xxx", "xxxxxx"); // The final sending method mailClient. send (message);} Response. write ("sent successfully"); Method 2: cs code + web. config in the web. add the following code to confg <system.net> <! -- If it is a third-party smtp server, you must specify the userName and password, and specify the sender email address from Based on the host. The test shows that the from value must be the userName value and the specified smpt server, it must be specified. If it is a local smtp server, just specify defaultCredentials = "true" --> <mailSettings> <smtp deliveryMethod = "Network" from = "xxxxxx@qq.com"> <network host = "smtp.qq.com" port = "25" userName = ""xxx" password = "xxxxxx"/> </smtp> </mailSettings> </system.net> using System. net. mail; using (MailMessage message = new MailMessa Ge () {message. to. add (new MailAddress ("xxxxxxx@qq.com"); // recipient email message. subject = "send mail through Configuration File Settings"; // mail Subject message. body = "here is the mail Body"; // mail Body SmtpClient mailClient = new SmtpClient (); mailClient. send (message); Response. write ("sent successfully");} method 3: cs code (function) using System. net. mail; using System. text; public bool SendEmail (string mailTo, string mailSubject, string mailContent) {// set the sender's mail information, such as using Netease's smtp string SmtpServer = "smtp.qq.com"; // SMTP Server string mailFrom = "XXXXX@qq.com"; // login username string userPassword = "XXX "; // login password // set SmtpClient smtpClient SmtpClient = new smtpClient (); SmtpClient. deliveryMethod = SmtpDeliveryMethod. network; // specify the email sending method smtpClient. host = smtpServer; // specify the SMTP server smtpClient. credentials = new System. net. networkCredential (mailFrom, userPassword); // username and password // set MailMessa Ge mailMessage = new MailMessage (mailFrom, mailTo); // sender and recipient mailMessage. subject = mailSubject; // Subject mailMessage. body = mailContent; // content mailMessage. bodyEncoding = Encoding. UTF8; // The Body code mailMessage. isBodyHtml = true; // set it to the HTML format mailMessage. priority = MailPriority. low; // priority try {smtpClient. send (mailMessage); // Send the MAIL return true;} catch (SmtpException ex) {return false;} bool result = this. send Email ("xxxxxxxx@qq.com", "EMAIL Subject", "Email content"); if (result) {Response. write ("sent successfully");} else {Response. write ("failed to send");} the above three methods have passed the test and are shared with 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.