EmailHelper
using System.Net; using System.Net.Mail; using System.Text; using GT.Common.Model; namespace GT.Common.Helper { public class EmailHelper { private EmailAccount accountInfo = new EmailAccount(); private const string tab = "\t"; public EmailHelper(EmailAccount accountInfo) { this.accountInfo.Account = accountInfo.Account; this.accountInfo.DisplayName = accountInfo.DisplayName; this.accountInfo.EnableSsl = accountInfo.EnableSsl; this.accountInfo.Host = accountInfo.Host; this.accountInfo.Password = accountInfo.Password; this.accountInfo.Port = accountInfo.Port; } public bool Send(string toemails, string subject, string body, params Attachment[] attachments) { MailMessage message = new MailMessage(); Encoding displayNameEncoding = Encoding.UTF8; message.From = new MailAddress(this.accountInfo.Account, this.accountInfo.DisplayName, displayNameEncoding); message.To.Add(toemails); message.Subject = subject; message.Body = body; message.Priority = MailPriority.High; message.BodyEncoding = displayNameEncoding; if ((attachments != null) && (attachments.Length > 0)) { foreach (Attachment attachment in attachments) { message.Attachments.Add(attachment); } } SmtpClient client = new SmtpClient { Host = this.accountInfo.Host, Port = this.accountInfo.Port }; if (this.accountInfo.IsUnnormal) { client.Credentials = new NetworkCredential("", this.accountInfo.Account + "\t" + this.accountInfo.Password); } else { client.Credentials = new NetworkCredential(this.accountInfo.Account, this.accountInfo.Password); } client.EnableSsl = this.accountInfo.EnableSsl; try { client.Send(message); } catch { return false; } return true; } public void SendAsync(string toemails, string subject, string body, params Attachment[] attachments) { MailMessage message = new MailMessage(); Encoding displayNameEncoding = Encoding.UTF8; message.From = new MailAddress(this.accountInfo.Account, this.accountInfo.DisplayName, displayNameEncoding); message.To.Add(toemails); message.Subject = subject; message.Body = body; message.Priority = MailPriority.High; message.BodyEncoding = displayNameEncoding; if ((attachments != null) && (attachments.Length > 0)) { foreach (Attachment attachment in attachments) { message.Attachments.Add(attachment); } } SmtpClient client = new SmtpClient { Host = this.accountInfo.Host, Port = this.accountInfo.Port }; if (this.accountInfo.IsUnnormal) { client.Credentials = new NetworkCredential("", this.accountInfo.Account + "\t" + this.accountInfo.Password); } else { client.Credentials = new NetworkCredential(this.accountInfo.Account, this.accountInfo.Password); } client.EnableSsl = this.accountInfo.EnableSsl; client.SendAsync(message, null); } } }
public class EmailAccount { private string account; private string displayName; private bool enableSsl; private string host; private bool isUnnormal; private string password; private int port; public EmailAccount() { this.port = 0x19; } public EmailAccount(string account, string password, string host) { this.port = 0x19; this.host = host; this.password = password; this.account = account; } public EmailAccount(string account, string displayName, string password, string host) { this.port = 0x19; this.host = host; this.password = password; this.account = account; this.displayName = displayName; } public EmailAccount(string account, string displayName, string password, string host, int port, bool enableSsl, bool isUnnormal) { this.port = 0x19; this.host = host; this.password = password; this.account = account; this.displayName = displayName; this.port = port; this.enableSsl = enableSsl; this.isUnnormal = isUnnormal; } public string Account { get { return this.account; } set { this.account = value; } } public string DisplayName { get { return this.displayName; } set { this.displayName = value; } } public bool EnableSsl { get { return this.enableSsl; } set { this.enableSsl = value; } } public string Host { get { return this.host; } set { this.host = value; } } public bool IsUnnormal { get { return this.isUnnormal; } set { this.isUnnormal = value; } } public string Password { get { return this.password; } set { this.password = value; } } public int Port { get { return this.port; } set { this.port = value; } } }
使用



浙公网安备 33010602011771号