C# 发送邮件
public class MailModel { /// <summary> /// 用户名,根据需要看是否需要带@hotmail.com /// </summary> public string UserName { get; set; } /// <summary> /// 密码 /// </summary> public string Password { get; set; } /// <summary> /// 服务器 /// </summary> public string Host { get; set; } /// <summary> /// 邮箱账户 /// </summary> public string FromMailAddress { get; set; } /// <summary> /// 目标账户 /// </summary> public IList<string> ToAddress { get; set; } /// <summary> /// 抄送账户 /// </summary> public IList<string> CcAddress { get; set; } /// <summary> /// 密送账户 /// </summary> public IList<string> BccAddress { get; set; } /// <summary> /// 是否开启安全连接 /// </summary> public bool EnableSsl { get; set; } /// <summary> /// 传输协议 /// </summary> public SmtpDeliveryMethod DeliveryMethod { get; set; } /// <summary> /// 端口 /// </summary> public int Port { get; set; } /// <summary> /// 标题 /// </summary> public string Subject { get; set; } /// <summary> /// 内容 /// </summary> public string Body { get; set; } /// <summary> /// 附件集合 /// </summary> public IList<string> MailFiles { get; set; } public MailPriority EMailPriority { get; set; } public MailModel() { ToAddress = new List<string>(); } }
public static class SendMailHelper { /// <summary> /// 发送邮件 /// </summary> /// <param name="mailModel"></param> public static void SendMail(MailModel mailModel) { try { SmtpClient smtp = new SmtpClient(); smtp.Host = mailModel.Host; smtp.EnableSsl = true; //开启安全连接。 smtp.Credentials = new NetworkCredential(mailModel.FromMailAddress, mailModel.Password); //创建用户凭证 smtp.DeliveryMethod = mailModel.DeliveryMethod;//SmtpDeliveryMethod.Network; //使用网络传送 MailMessage message = new MailMessage(); //创建邮件 //邮件采用的编码 message.BodyEncoding = Encoding.UTF8; ////设置邮件的优先级为高 message.Priority = mailModel.EMailPriority; message.From = new MailAddress(mailModel.FromMailAddress, mailModel.UserName, Encoding.UTF8); message.IsBodyHtml = true; message.Subject = mailModel.Subject; message.Body = mailModel.Body; if (mailModel.ToAddress != null && mailModel.ToAddress.Any()) { foreach (var to in mailModel.ToAddress) { //发送 message.To.Add(to); } } if (mailModel.CcAddress != null && mailModel.CcAddress.Any()) { foreach (var cc in mailModel.CcAddress) { //抄送 message.CC.Add(cc); } } if (mailModel.BccAddress != null && mailModel.BccAddress.Any()) { foreach (var bcc in mailModel.BccAddress) { //密送 message.Bcc.Add(bcc); } } if (mailModel.MailFiles != null && mailModel.MailFiles.Any()) { foreach (var fs in mailModel.MailFiles) { //var fsMapPath = HttpContext.Current.Server.MapPath(fs);//获得附件在本地地址 //将文件进行转换成Attachments //Attachment data = new Attachment(fs, MediaTypeNames.Application.Octet); //// Add time stamp information for the file. //ContentDisposition disposition = data.ContentDisposition; //disposition.CreationDate = System.IO.File.GetCreationTime(fs); //disposition.ModificationDate = System.IO.File.GetLastWriteTime(fs); //disposition.ReadDate = System.IO.File.GetLastAccessTime(fs); //message.Attachments.Add(data); //附件 //message.Attachments.Add(new Attachment(fs, MediaTypeNames.Application.Octet)); FileInfo fileInfo = new FileInfo(fs); message.Attachments.Add(new Attachment(new MemoryStream(File.ReadAllBytes(fileInfo.FullName)), fileInfo.FullName, MediaTypeNames.Application.Octet)); } } smtp.Send(message); //发送邮件 ////释放资源 message.Dispose(); } catch (SmtpFailedRecipientsException) { } catch (SmtpFailedRecipientException) { } catch (SmtpException) { } } }