using System; using System.Collections.Generic; using System.Text; using System.Net.Mail; using System.Net.Mime; namespace Stone.Common { public class MySmtpMail { private MailMessage mailMessage; private SmtpClient smtpClient; private string server; private string username; private string password; private bool enablessl; private bool credentials; private int port; public MySmtpMail(string To, string From, string Body, string Title, string Server, string Username, string Password, bool EnableSsl, int Port, bool Credentials, string AttachmentFile) { mailMessage = new MailMessage(); mailMessage.To.Add(To); mailMessage.From = new System.Net.Mail.MailAddress(From); mailMessage.Subject = Title; mailMessage.Body = Body; if(AttachmentFile != "") { Attachment attachment = new Attachment(AttachmentFile); mailMessage.Attachments.Add(attachment); } mailMessage.IsBodyHtml = true; mailMessage.BodyEncoding = System.Text.Encoding.UTF8; mailMessage.Priority = System.Net.Mail.MailPriority.Normal; this.server = Server; this.password = Password; this.username = Username; this.enablessl = EnableSsl; this.port = Port; this.credentials = Credentials; } public void Send() { if (mailMessage != null) { smtpClient = new SmtpClient(); if (!credentials) { smtpClient.Credentials = new System.Net.NetworkCredential(username, password); } smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtpClient.Host = server; smtpClient.Port = port; smtpClient.EnableSsl = enablessl; smtpClient.Send(mailMessage); } } #region 暂时不用的 public void Attachments(string Path) { string[] path = Path.Split(','); Attachment data; ContentDisposition disposition; for (int i = 0; i < path.Length; i++) { data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化[shi li hua] disposition = data.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取 disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);// disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附 mailMessage.Attachments.Add(data);//添加到附件中 } } public void SendAsync(SendCompletedEventHandler CompletedMethod) { if (mailMessage != null) { smtpClient = new SmtpClient(); smtpClient.Credentials = new System.Net.NetworkCredential (mailMessage.From.Address, password);//设置[she zhi]发件人身份[shen fen]的票据 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtpClient.Host = "smtp." + mailMessage.From.Host; smtpClient.SendCompleted += new SendCompletedEventHandler (CompletedMethod);//注册[zhu ce]异步[yi bu]发送邮件[you jian]完成时的事件[shi jian] smtpClient.SendAsync(mailMessage, mailMessage.Body); } } #endregion } }