a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

c#異步發(fā)送郵件的類

2020-01-24 02:51:35
字體:
供稿:網(wǎng)友

首先要定義一個郵件信息的基類,如下所示:

復(fù)制代碼 代碼如下:

/// <summary>
/// Base message class used for emails
/// </summary>
public class Message
{
#region Constructor
/// <summary>
/// Constructor
/// </summary>
public Message()
{
}
#endregion

#region Properties
/// <summary>
/// Whom the message is to
/// </summary>
public virtual string To { get; set; }

/// <summary>
/// The subject of the email
/// </summary>
public virtual string Subject { get; set; }

/// <summary>
/// Whom the message is from
/// </summary>
public virtual string From { get; set; }

/// <summary>
/// Body of the text
/// </summary>
public virtual string Body { get; set; }

#endregion
}

然后定義一個郵件的發(fā)送類,使用Netmail的方式發(fā)送郵件,發(fā)送郵件時采用了.net中自帶的線程池,
通過多線程來實現(xiàn)異步的發(fā)送,代碼如下:

復(fù)制代碼 代碼如下:

 /// <summary>
/// Utility for sending an email
/// </summary>
public class EmailSender : Message
{
#region Constructors

/// <summary>
/// Default Constructor
/// </summary>
public EmailSender()
{
Attachments = new List<Attachment>();
EmbeddedResources = new List<LinkedResource>();
Priority = MailPriority.Normal;
}

#endregion

#region Public Functions

/// <summary>
/// Sends an email
/// </summary>
/// <param name="Message">The body of the message</param>
public void SendMail(string Message)
{
Body = Message;
SendMail();
}

/// <summary>
/// Sends a piece of mail asynchronous
/// </summary>
/// <param name="Message">Message to be sent</param>
public void SendMailAsync(string Message)
{
Body = Message;
ThreadPool.QueueUserWorkItem(delegate { SendMail(); });
}

/// <summary>
/// Sends an email
/// </summary>
public void SendMail()
{
using (System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage())
{
char[] Splitter = { ',', ';' };
string[] AddressCollection = To.Split(Splitter);
for (int x = 0; x < AddressCollection.Length; ++x)
{
if(!string.IsNullOrEmpty(AddressCollection[x].Trim()))
message.To.Add(AddressCollection[x]);
}
if (!string.IsNullOrEmpty(CC))
{
AddressCollection = CC.Split(Splitter);
for (int x = 0; x < AddressCollection.Length; ++x)
{
if (!string.IsNullOrEmpty(AddressCollection[x].Trim()))
message.CC.Add(AddressCollection[x]);
}
}
if (!string.IsNullOrEmpty(Bcc))
{
AddressCollection = Bcc.Split(Splitter);
for (int x = 0; x < AddressCollection.Length; ++x)
{
if (!string.IsNullOrEmpty(AddressCollection[x].Trim()))
message.Bcc.Add(AddressCollection[x]);
}
}
message.Subject = Subject;
message.From = new System.Net.Mail.MailAddress((From));
AlternateView BodyView = AlternateView.CreateAlternateViewFromString(Body, null, MediaTypeNames.Text.Html);
foreach (LinkedResource Resource in EmbeddedResources)
{
BodyView.LinkedResources.Add(Resource);
}
message.AlternateViews.Add(BodyView);
//message.Body = Body;
message.Priority = Priority;
message.SubjectEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
message.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
message.IsBodyHtml = true;
foreach (Attachment TempAttachment in Attachments)
{
message.Attachments.Add(TempAttachment);
}
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(Server, Port);
if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))
{
smtp.Credentials = new System.Net.NetworkCredential(UserName, Password);
}
if (UseSSL)
smtp.EnableSsl = true;
else
smtp.EnableSsl = false;
smtp.Send(message);
}
}

/// <summary>
/// Sends a piece of mail asynchronous
/// </summary>
public void SendMailAsync()
{
ThreadPool.QueueUserWorkItem(delegate { SendMail(); });
}

#endregion

#region Properties

/// <summary>
/// Any attachments that are included with this
/// message.
/// </summary>
public List<Attachment> Attachments { get; set; }

/// <summary>
/// Any attachment (usually images) that need to be embedded in the message
/// </summary>
public List<LinkedResource> EmbeddedResources { get; set; }

/// <summary>
/// The priority of this message
/// </summary>
public MailPriority Priority { get; set; }

/// <summary>
/// Server Location
/// </summary>
public string Server { get; set; }

/// <summary>
/// User Name for the server
/// </summary>
public string UserName { get; set; }

/// <summary>
/// Password for the server
/// </summary>
public string Password { get; set; }

/// <summary>
/// Port to send the information on
/// </summary>
public int Port { get; set; }

/// <summary>
/// Decides whether we are using STARTTLS (SSL) or not
/// </summary>
public bool UseSSL { get; set; }

/// <summary>
/// Carbon copy send (seperate email addresses with a comma)
/// </summary>
public string CC { get; set; }

/// <summary>
/// Blind carbon copy send (seperate email addresses with a comma)
/// </summary>
public string Bcc { get; set; }

#endregion
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 亚洲三级视频 | 精品日韩在线 | 欧美一区二区精品 | 黄色自拍视频 | 中文字幕亚洲一区 | 亚洲 欧美日韩 国产 中文 | zzzwww在线观看免 | 久产久精品 | 伊人免费在线观看高清版 | 欧美精品日韩 | 日韩中文久久 | 免费小视频 | 亚洲蜜桃精久久久久久久 | 欧美三级 欧美一级 | 国产不卡福利片 | 国产欧美精品一区二区三区四区 | av资源中文在线天堂 | 色婷婷在线视频 | 欧美午夜寂寞影院 | 日本中文字幕一区二区 | 久久99精品久久久噜噜最新章节 | 精品国产乱码久久久久久久 | 91.成人天堂一区 | 亚洲成人av在线 | 午夜精品久久久久久久久久久久 | 国产精品久久久久久久久久久久久 | 一二三区字幕免费观看av | 国产精品久久久久久一区二区三区 | 久久草| 在线观看xxx | 视频一区二区三 | 国产片一区二区三区 | 老司机在线精品视频 | 久久久久久久国产 | 欧洲中文字幕 | 亚洲成人精品 | 久热久热 | 久久伊人国产 | 精品久久久久久久 | 日韩欧美国产精品一区二区三区 | 亚洲国产精品一区 |