In previous projects, the Web site was designed to send mail, and a piece of code was looked up on the internet for a moment:
private static void SendEmail (String clienthost, String EmailAddress, String re Ceiveaddress, String userName, string password, string subject, String body) {MailMessage mail = new MailMessage (); Mail. from = new MailAddress (emailaddress); Mail. To.add (New MailAddress (receiveaddress)); Mail. Subject = Subject; Mail. BODY = body; Mail. Isbodyhtml = true; Mail. Deliverynotificationoptions = deliverynotificationoptions.onfailure; SmtpClient client = new SmtpClient (); Client. Host = ClientHost; Client. Credentials = new NetworkCredential (userName, password); Client. Deliverymethod = Smtpdeliverymethod.network; try {client. SendAsync (Mail,null); } catch (Exception) {}}
In the local test no problem, but the deployment to the window server2012 can not send, and then on the internet Baidu a bunch, configure the mail server is not satisfactory, and later found a deprecated code is normal:
public static void SendEmail (string from, string subject, String body, list<string> mailaddress, string host, int Port, string password) {System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage (); try {mail. to = string. Join (",", mailaddress); Mail. from = from; Mail. Subject = Subject; Mail. BodyFormat = System.Web.Mail.MailFormat.Html; Mail. BODY = body; Mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); Authentication mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendusername", mail. from); Email login, here is the same as the previous send account mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", password); This password to note: If it is a general account, to use the authorization code, Enterprise account with login password mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//Port Mail. FielDs. ADD ("Http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//ssl encryption System.web.mail.smtpmail.s Mtpserver = "smtp.qq.com"; Enterprise account with smtp.exmail.qq.com System.Web.Mail.SmtpMail.Send (Mail); Message sent successfully} catch (Exception ex) {//failure, error message: Ex. Message; } }
Later discovered may be because the server is set up in the intranet, the specific port is not open or some restrictions, but because of the work requirements, temporarily can only step backward, must be corrected later.
. NET MVC Mail Delivery