123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Net;
- using System.Net.Mail;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Xml.Linq;
- namespace FCS.Common
- {
- public class EmailHelper
- {
- private static Email model;
- static EmailHelper()
- {
- #region 获取配置数据
- string configFile = AppDomain.CurrentDomain.BaseDirectory + "/XmlConfig/Email.xml";
- model = CommonHelper.ConvertXMLToObject<Email>(configFile, "UrlSetting")[0] as Email;
-
- #endregion
- }
- /// <summary>
- /// 发送邮件到指定收件人
- /// </summary>
- /// <param name="to">收件人地址</param>
- /// <param name="subject">主题</param>
- /// <param name="mailBody">正文内容(支持HTML)</param>
- /// <param name="copyTos">抄送地址列表</param>
- /// <returns>是否发送成功</returns>
- public static bool Send(string to, string subject, string mailBody, params string[] copyTos)
- {
- return Send(new[] { to }, subject, mailBody, copyTos, new string[] { }, MailPriority.Normal);
- }
- /// <summary>
- /// 发送邮件到指定收件人
- /// </summary>
- /// <remarks>
- /// </remarks>
- /// <param name="tos">收件人地址列表</param>
- /// <param name="subject">主题</param>
- /// <param name="mailBody">正文内容(支持HTML)</param>
- /// <param name="ccs">抄送地址列表</param>
- /// <param name="bccs">密件抄送地址列表</param>
- /// <param name="priority">此邮件的优先级</param>
- /// <param name="attachments">附件列表</param>
- /// <returns>是否发送成功</returns>
- /// <exception cref="System.ArgumentNullException">attachments</exception>
- public static bool Send(string[] tos, string subject, string mailBody, string[] ccs, string[] bccs, MailPriority priority, params Attachment[] attachments)
- {
- try
- {
- Encoding encode = Encoding.UTF8;
- SmtpClient smtp = new SmtpClient();
- smtp.DeliveryMethod = SmtpDeliveryMethod.Network;//将smtp的出站方式设为 Network
- smtp.Host = model.SmtpServer;//指定 smtp 服务器地址
- smtp.Port = model.SmtpServerPort;//指定 smtp 服务器的端口,默认是25,如果采用默认端口,可省去
- //如果需要认证,则用此的方式
- smtp.EnableSsl = model.SmtpEnableSsl;//smtp服务器是否启用SSL加密
- //如果你的SMTP服务器不需要身份认证,则使用下面的方式,不过,目前基本没有不需要认证的了
- smtp.UseDefaultCredentials = true;
- smtp.Credentials = new NetworkCredential(model.SmtpUsername, model.SmtpUserPassword);
- MailMessage mail = new MailMessage();
- mail.Priority = MailPriority.High; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可
- MailAddress ma = new MailAddress(model.SmtpUsername, model.SmtpDisplayName, encode);
- //收件方看到的邮件来源;
- //第一个参数是发信人邮件地址
- //第二参数是发信人显示的名称
- //第三个参数是 第二个参数所使用的编码,如果指定不正确,则对方收到后显示乱码
- //936是简体中文的codepage值
- //注:上面的邮件来源,一定要和你登录邮箱的帐号一致,否则会认证失败
- mail.From = ma;
- //邮件的接收者,支持群发,多个地址之间用(半角逗号)分开
- foreach (var acceptMail in tos)
- {
- mail.To.Add(acceptMail);
- }
- if (ccs != null)
- foreach (var acceptMail in ccs)
- {
- mail.Bcc.Add(acceptMail);
- }
- mail.SubjectEncoding = encode;
- mail.Subject = subject;
- mail.IsBodyHtml = true;//是否启用html代码
- mail.BodyEncoding = encode;
- mail.Body = mailBody;
- smtp.Send(mail);
- Trace.WriteLine("发送成功!");
- return true;
- }
- catch (Exception ex)
- {
- Thread.CurrentThread.Abort();
- Trace.WriteLine("发送失败!");
- return false;
- }
- }
- }
- public class Email
- {
- public string SmtpServer { get; set; }//SMTP服务器
- public int SmtpServerPort { get; set; }//SMTP端口
- public bool SmtpEnableSsl { get; set; }//是否SSL加密连接
- public string SmtpUsername { get; set; }//发件人邮箱地址
- public string SmtpDisplayName { get; set; }//发件人昵称
- public string SmtpUserPassword { get; set; }//授权码
- }
- }
|