调用DEMO
var currUser = new List<string> { "123@qq.com" , "123@qq.com" , "123@qq.com" };// 单个 var title = "test"; var content = "hello word"; mh.SendSMTPEMail(currUser, title, content);
方法:
public class MailHelper { private string emailAcount = ConfigurationManager.AppSettings["EmailAcount"]; private string emailPassword = ConfigurationManager.AppSettings["EmailPassword"]; private string emailSmart = ConfigurationManager.AppSettings["EmailSmart"]; public void SendSMTPEMail(string strto, string strSubject, string strBody) { System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(emailSmart); client.UseDefaultCredentials = false; client.EnableSsl = true; client.Credentials = new System.Net.NetworkCredential(emailAcount, emailPassword); client.DeliveryMethod = SmtpDeliveryMethod.Network; System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(emailAcount, strto, strSubject, strBody); message.BodyEncoding = System.Text.Encoding.UTF8; message.IsBodyHtml = true; //使用文件路径发送附件 //AlternateView item = new AlternateView(@"D:\\软件\\(软电话)eyebeam1.5.rar", "application/x-rar-compressed"); //message.AlternateViews.Add(item); //message.AlternateViews.Dispose(); //使用Stream发送附件 //Attachment letter = new Attachment(FileUploadLetter.FileContent, FileUploadLetter.PostedFile.ContentType); //letter.ContentDisposition.Inline = true; //letter.ContentDisposition.DispositionType = DispositionTypeNames.Inline; ////inline.ContentId = "1"; //letter.ContentType.MediaType = FileUploadLetter.PostedFile.ContentType; //letter.ContentType.Name = Path.GetFileName(FileUploadLetter.PostedFile.FileName); //letter.Name = Path.GetFileName(FileUploadLetter.PostedFile.FileName); //message.Attachments.Add(letter); //message.Attachments.Dispose(); client.Send(message); } public void SendSMTPEMail(List<string> strto, string strSubject, string strBody) { System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(emailSmart); client.UseDefaultCredentials = false; client.EnableSsl = true; client.Credentials = new System.Net.NetworkCredential(emailAcount, emailPassword); client.DeliveryMethod = SmtpDeliveryMethod.Network; foreach (var str in strto) { try { System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(emailAcount, str, strSubject, strBody); message.BodyEncoding = System.Text.Encoding.UTF8; message.IsBodyHtml = true; client.Send(message); } catch { } } } }
1.补充知识
(1)POP3和SMTP服务器是什么?
来源:https://www.cnblogs.com/0to9/p/7986815.html