Asp.net mvc sends an email, asp. netmvc

Source: Internet
Author: User

Asp.net mvc sends an email, asp. netmvc

References:

Article 1: http://www.cnblogs.com/qinpengming/archive/2011/06/08/2075040.html

Article 2: http://www.cnblogs.com/XuebinDing/archive/2012/03/27/2419497.html

Article 3: http://jingyan.baidu.com/article/0f5fb099dffe7c6d8334ea31.html (steps to enable smtp service in QQ Mail)

Article 4: http://bbs.csdn.net/topics/370021675

The addresses of POP3 and SMTP servers in qq mail are set as follows: the POP3 server (port 110) SMTP server (port 25) qq. compop. qq. comsmtp. qq. comSMTP server requires authentication.

For POP3 and smtp ssl encryption, the port is as follows:

POP3 server (Port 995)

SMTP server (Port 465 or 587 ).

To enable the following service in qq mail, qq will give you an authorization code, which details in the following code where to use this authorization code.

Controller code:

1 using System. Net. Mail; // The email must be referenced

1 public ActionResult bindStep3 (ModelCompany c) 2 {3 ......... 4 .......... 5 ........... 6 MailAddress MessageFrom = new MailAddress ("xxxxx@qq.com"); // sender email address 7 string MessageTo = company. COMPANY_EMAIL; // recipient's email address 8 string MessageSubject = "activation Verification"; // Subject 9 string MessageBody = "Please verify your email to complete the last step of registration, click the following link to activate your account: <br> <a target = '_ blank' rel = 'nofollow' style = 'color: #0041D3; text-decoration: underline 'Href = ''> activate </a>"; // email content (generally a URL link, which generates a random number and adds the verification id parameter. Click go to the website for verification .) 10 11 if (SendMail (MessageFrom, MessageTo, MessageSubject, MessageBody) 12 {13 Response. write ("<script type = 'text/javascript '> alert ('email sending failed'); </script>"); 14} 15 else16 {17 Response. write ("<script type = 'text/javascript '> alert ('email sending failed'); </script> "); 18} 19 TempData ["CompanyBind3"] = company; 20 return View ("bindStep3"); 21} 22 23 public bool SendMail (MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody) // send verification email 24 {25 MailMessage message = new MailMessage (); 26 message. to. add (MessageTo); 27 message. from = MessageFrom; 28 message. subject = MessageSubject; 29 message. subjectEncoding = System. text. encoding. UTF8; 30 message. body = MessageBody; 31 message. bodyEncoding = System. text. encoding. UTF8; 32 message. isBodyHtml = true; // whether it is in html format 33 message. priority = MailPriority. high; // priority 34 SmtpClient SC = new SmtpClient (); 35 SC. enableSsl = true; // whether SSL encryption 36 SC. host = "smtp.qq.com"; // specify the server address or IP address of the email 37 SC. port = 587; // specify the mail sending Port 38 SC. credentials = new System. net. networkCredential ("xxxxx@qq.com", "xxxxx"); // specify the username and password of the login server (note: the password here is the authorization password provided to you by the pop3/smtp service, not your QQ password) 39 40 try41 {42 SC. send (message); // Send email 43} 44 catch (Exception e) 45 {46 Response. write (e. message); 47 return false; 48} 49 return true; 50 51}View Code

Effect:

 

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.