栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

c#异步发送邮件的类

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c#异步发送邮件的类

首先要定义一个邮件信息的基类,如下所示:
复制代码 代码如下:
///


/// base message class used for emails
///

public class Message
{
#region Constructor
///
/// Constructor
///

public Message()
{
}
#endregion

#region Properties
///


/// Whom the message is to
///

public virtual string To { get; set; }

///


/// The subject of the email
///

public virtual string Subject { get; set; }

///


/// Whom the message is from
///

public virtual string From { get; set; }

///


/// Body of the text
///

public virtual string Body { get; set; }

#endregion
}

然后定义一个邮件的发送类,使用Netmail的方式发送邮件,发送邮件时采用了.net中自带的线程池,
通过多线程来实现异步的发送,代码如下:
复制代码 代码如下:
 ///


/// Utility for sending an email
///

public class EmailSender : Message
{
#region Constructors

///


/// Default Constructor
///

public EmailSender()
{
Attachments = new List();
EmbeddedResources = new List();
Priority = MailPriority.Normal;
}

#endregion

#region Public Functions

///


/// Sends an email
///

/// The body of the message
public void SendMail(string Message)
{
Body = Message;
SendMail();
}

///


/// Sends a piece of mail asynchronous
///

/// Message to be sent
public void SendMailAsync(string Message)
{
Body = Message;
ThreadPool.QueueUserWorkItem(delegate { SendMail(); });
}

///


/// Sends an email
///

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);
}
}

///


/// Sends a piece of mail asynchronous
///

public void SendMailAsync()
{
ThreadPool.QueueUserWorkItem(delegate { SendMail(); });
}

#endregion

#region Properties

///


/// Any attachments that are included with this
/// message.
///

public List Attachments { get; set; }

///


/// Any attachment (usually images) that need to be embedded in the message
///

public List EmbeddedResources { get; set; }

///


/// The priority of this message
///

public MailPriority Priority { get; set; }

///


/// Server Location
///

public string Server { get; set; }

///


/// User Name for the server
///

public string UserName { get; set; }

///


/// Password for the server
///

public string Password { get; set; }

///


/// Port to send the information on
///

public int Port { get; set; }

///


/// Decides whether we are using STARTTLS (SSL) or not
///

public bool UseSSL { get; set; }

///


/// Carbon copy send (seperate email addresses with a comma)
///

public string CC { get; set; }

///


/// Blind carbon copy send (seperate email addresses with a comma)
///

public string Bcc { get; set; }

#endregion
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/126673.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号