using System; using System.Text; using System.Net; using System.Net.Mail; using System.Net.Mime; using System.Threading; namespace JobSendAgentMail { /// /// 邮件发送类 /// public class SendEmail { private MailMessage mailMessage = new MailMessage(); //实例化一个邮件类 #region 构造函数 /// /// 构造函数 /// /// 收件人地址(多个以,号分开) /// 发件人地址 /// 主题 /// 正文 public SendEmail(string toAddresses, string fromAddress, string title, string body) : this(toAddresses, fromAddress, "", "", title, body, false) { } /// /// 构造函数 /// /// 收件人地址 /// 发件人地址 /// 收件人名字 /// 发件人姓名 /// 主题 /// 正文 /// 正文是否为html格式 public SendEmail(string toAddress, string fromAddress, string toName, string fromName, string title, string body, bool isBodyHtml) { mailMessage.From = new MailAddress(fromAddress, fromName, Encoding.GetEncoding(936)); if (toName.Equals("")) mailMessage.To.Add(toAddress); else mailMessage.To.Add(new MailAddress(toAddress, toName, Encoding.GetEncoding(936))); mailMessage.Subject = title; mailMessage.SubjectEncoding = Encoding.GetEncoding(936); mailMessage.Body = body; mailMessage.IsBodyHtml = isBodyHtml; mailMessage.BodyEncoding = Encoding.GetEncoding(936); } #endregion /// /// 设置SMTP,并且将邮件发送出去 /// 所有参数都设置完成后再调用该方法 /// /// 发件人密码 /// SMTP服务器地址 public void SetSmtp(string password, string smtpHost) { SetSmtp(mailMessage.From.Address, password, smtpHost, 25, false, MailPriority.Normal); } /// /// 设置SMTP,并且将邮件发送出去 /// 所有参数都设置完成后再调用该方法 /// /// 发件人地址(必须为真实有效的email地址) /// 发件人密码 /// SMTP服务器地址 /// SMTP服务器的端口 /// SMTP服务器是否启用SSL加密 /// 邮件的优先级 public void SetSmtp(string address, string password, string smtpHost, int smtpPort, bool isEnableSsl, MailPriority priority) { SmtpClient smtp = new SmtpClient(smtpHost,smtpPort); // smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // smtp.UseDefaultCredentials = true; //if (isEnableSsl) //{ // smtp.UseDefaultCredentials = true; // System.Net.NetworkCredential nc = new System.Net.NetworkCredential(address, password); // smtp.Credentials = nc.GetCredential(smtpHost, smtpPort, "NTLM"); //} //else System.Net.NetworkCredential nc = new System.Net.NetworkCredential(address, password); smtp.Credentials = nc; //smtp.Credentials = new NetworkCredential(address, password); //smtp.Host = smtpHost; //smtp.Port = smtpPort; smtp.EnableSsl = isEnableSsl; // smtp.UseDefaultCredentials = true; // mailMessage.Priority = priority; try { smtp.Send(mailMessage); //发送邮件 } catch (Exception e) { smtp.Dispose(); } finally { smtp.Dispose(); } smtp.Dispose(); } class Mailinfo { public SendEmail se { get; set; } public string address { get; set; } public string password { get; set; } public string smtpHost { get; set; } public int smtpPort { get; set; } public bool isEnableSsl { get; set; } public MailPriority priority { get; set; } public Mailinfo(SendEmail se, string address, string password, string smtpHost, int smtpPort, bool isEnableSsl, MailPriority priority) { this.se = se; this.address = address; this.password = password; this.smtpHost = smtpHost; this.smtpPort = smtpPort; this.isEnableSsl = isEnableSsl; this.priority = priority; } } class ThreadMail { public void FuncSend(object o) { Mailinfo Mailinfo = o as Mailinfo; Mailinfo.se.SetSmtp(Mailinfo.address, Mailinfo.password, Mailinfo.smtpHost, Mailinfo.smtpPort, Mailinfo.isEnableSsl, Mailinfo.priority); } } public void SetSmtp_thread( string address, string password, string smtpHost, int smtpPort, bool isEnableSsl, MailPriority priority) { ThreadMail tmail = new ThreadMail(); //有参调用实例方法,ParameterizedThreadStart是一个委托,input为object,返回值为void Thread thread1 = new Thread(new ParameterizedThreadStart(tmail.FuncSend)); Mailinfo mi = new Mailinfo(this, address, password, smtpHost, smtpPort, isEnableSsl, priority); thread1.Start(mi); } #region //设置邮件地址 /// /// 设置更多收件人 /// /// 收件人地址 public void SetMoreToAddress(string toAddresses) { mailMessage.To.Add(toAddresses); } /// /// 设置更多收件人 /// /// 收件人地址 /// 收件人名字 public void SetMoreToAddress(string toAddress, string toName) { mailMessage.To.Add(new MailAddress(toAddress, toName, Encoding.GetEncoding(936))); } /// /// 设置抄送者(多个以,号分开) /// /// 抄送者地址 public void SetCarbonCopyFor(string ccAddresses) { mailMessage.CC.Add(ccAddresses); } /// /// 设置抄送者 /// /// 抄送者地址 /// 抄送者名字 public void SetCarbonCopyFor(string ccAddress, string ccName) { mailMessage.Bcc.Add(new MailAddress(ccAddress, ccName, Encoding.GetEncoding(936))); } /// /// 设置密送者(多个以,号分开) /// /// 密送者 public void SetBlindCarbonCopyFor(string bccAddresses) { mailMessage.Bcc.Add(bccAddresses); } /// /// 设置密送者 /// /// 密送者 /// 密送者名字 public void SetBlindCarbonCopyFor(string bccAddress, string bccName) { mailMessage.Bcc.Add(new MailAddress(bccAddress, bccName, Encoding.GetEncoding(936))); } #endregion #region 添加附件 /// /// 添加附件(自动识别文件类型) /// /// 单个文件的路径 public void Attachments(string fileName) { mailMessage.Attachments.Add(new Attachment(fileName)); } /// /// 添加附件(默认为富文本RTF格式) /// /// 单个文件的路径 public void AttachmentsForRTF(string fileName) { mailMessage.Attachments.Add(new Attachment(fileName, MediaTypeNames.Application.Rtf)); } #endregion } }