Today, in the project, we need to realize the function of sending mailbox to activate the user's mailbox!
Before the test, we used personal email to test, everything was very smooth. Later, we changed it into the company's corporate email, and encountered a small problem, as follows:
Failed to send message due to incorrect command order The server response is: error: need Ehlo and auto first!
Later, through Baidu, we found the reason: you need to set EnableSsl and UseDefaultCredentials first, and then instantiate Credentials
The email code of the sending enterprise is as follows:
1 /// <summary> 2 /// Send mailbox 3 /// </summary> 4 /// <param name="mail">Destination email address</param> 5 /// <param name="Host">Activation address domain name</param> 6 /// <param name="UserID">User ID ID</param> 7 /// <param name="ActiCode">Randomly generate mailbox activation code</param> 8 public static void SendEmail(string mail, string Host, int? UserID, string ActiCode) 9 { 10 //Splicing mailbox activation link 11 formto = string.Format(formto, Host, UserID, ActiCode); 12 MailMessage mailMsg = new MailMessage(); 13 mailMsg.From = new MailAddress(name);//Source email address ,Sender 14 mailMsg.To.Add(new MailAddress(mail));//Destination email address. Can have multiple recipients. 15 mailMsg.Subject = "Display expert email activation verification";//Send the title of the message 16 mailMsg.Body = "Please verify your email,To activate the email you used to receive relevant information in the display expert, click the following link to activate your email:<br><a target='_blank' style='color:#0041D3;text-decoration:underline' href='" + formto + "'>Please click activate</a>";//Send message content 17 mailMsg.IsBodyHtml = true;//Whether to support HTML 18 SmtpClient client = new SmtpClient();//smtp.163.com,smtp.qq.com,smtp.exmail.qq.com(qq Enterprise mailbox smtp)Of the mailbox used by the sender SMTP The server. 19 client.Host = "smtp.exmail.qq.com";//Set the corresponding smtpserver 20 client.EnableSsl = true; 21 client.UseDefaultCredentials = false; 22 client.Credentials = new System.Net.NetworkCredential(name, upass);//Specify the sender's email account and password. 23 client.DeliveryMethod = SmtpDeliveryMethod.Network; 24 try 25 { 26 client.Send(mailMsg);//Queued mail. 27 } 28 catch (Exception ex) 29 { 30 throw; 31 } 32 }
Call method: SendEmail(email, Host, AdminUser.UserID, code);
The test effect diagram is as follows:
Remember the problems encountered in a job!