通过.NET实现后台自动发送邮件功能的代码,可以将一些基础信息放到web.config文件中进行保存:
Web.config文件信息段:
代码<system.net> <mailSettings> <smtp deliveryMethod="Network" from=" yanggl@hotmail.com "> <network host="smtp.hotmail.com" userName="yanggl@hotmail.com" password="" defaultCredentials="true" /> </smtp> </mailSettings> </system.net>
后台实现:
代码using System.Net.Mail; using System.Configuration; using System.Net.Configuration; using System.Web.Configuration; // addresses for send email. string[] address = new string[] { "guilin_gavin@hotmail.com", "glyang@isoftstone.com" }; // email body. string emailBody = "Hello! Guys!"; // email subject. string subject = "This is a test!"; // get the config info. SmtpSection smtpSection = NetSectionGroup.GetSectionGroup(WebConfigurationManager.OpenWebConfiguration("~/web.config")).MailSettings.Smtp; // save the mail object. MailMessage mm = new MailMessage(); foreach (string item in address) mm.To.Add(item); mm.From = new MailAddress(smtpSection.From); mm.BodyEncoding = System.Text.Encoding.UTF8; mm.SubjectEncoding = System.Text.Encoding.UTF8; mm.IsBodyHtml = true; mm.Body = emailBody; mm.Subject = subject; // set and send email. SmtpClient sc = new SmtpClient(smtpSection.Network.Host); sc.DeliveryMethod = SmtpDeliveryMethod.Network; sc.Credentials = new System.Net.NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password); sc.Send(mm);