<appSettings> <add key="EmailAcount" value="xxxx"/> <add key="EmailPassword" value="xxxx"/> <add key="Mail_Smtp" value="xxxx"/> </appSettings>
注意:輸入郵箱的先後空格服務器
private void NewMethod(string emailSendFrom, string emailSendFromTo, string subject, string content) { var emailAcount = ConfigurationManager.AppSettings["EmailAcount"];//獲取發送郵箱用戶名 var emailPassword = ConfigurationManager.AppSettings["EmailPassword"];//獲取發送郵箱密碼 var emailSmtp = ConfigurationManager.AppSettings["Mail_Smtp"];//獲取郵箱發送服務器 MailMessage message = new MailMessage(emailSendFrom, emailSendFromTo); //建立郵箱對象 //MailAddress fromAddr = new MailAddress("sunhaiming@cn.wilmar-intl.com");//發件人 //message.From = fromAddr; //message.To.Add(reciver); message.Subject = subject; message.Body = content; message.BodyEncoding = Encoding.UTF8; SmtpClient client = new SmtpClient(emailSmtp, 587);//smtp端口 client.Credentials = new NetworkCredential(emailAcount, emailPassword);//獲取郵箱用戶名及密碼 client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; client.EnableSsl = true;//判斷驗證方式 try { client.Send(message);//發送信息 } catch (System.Net.Mail.SmtpException ex) { MessageBox.Show(ex.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } }