搜索
简帛阁>技术文章>c#异步发送邮件的类

c#异步发送邮件的类

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

代码如下:

/// <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
}

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

代码如下:

 /// <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
}

首先要定义一个邮件信息,如下所示:代码如下:///<summary>///Basemessageclassusedforemails///</summary>publi
一、两种发送邮件方法有用到两种方式发邮件,一种是用SystemWebMail,另一种是SystemNetMail。SystemNetMail是作为SystemWebMail替代存在。Sys
本文实例为大家分享了Android九宫格图片展示具体代码,供大家参考,具体内容如下region发送邮件部分privatestaticStringfromMail1111@126com;///邮箱名称
微软已经为我们准备好了现成工具供我们调用:MailMessage//邮件信息SmtpClient//邮件发送首先需要在项目文件中引用以下命名空间:usingSystemNet;usingS
发送邮件由于是一个耗时操作,有可能需要一个几十秒操作,但是呢,接口是一个瞬间完成,为了不影响接口性能,所以需要对发送邮件操作进行异步操作,我们这里呢,首先我们要引入发送邮件测试模块。<
利用Django框架发送邮件详细过程,在前两天博客中有所记录,但是单纯那样发邮件是有非常大问题,这就需要celery异步发送来解决首先我们来看一下邮件发送过程:Django网站先发送到s
功能包括发送邮件,邮箱格式是否正确,和在不发送邮件情况下判断邮箱用户名和密码是否正确,鉴于POP检查邮箱用户名和密码出现错误情况返回结果延迟,用异步线程解决此问题,见代码:usingSyst
篇文章主要介绍如何使用CMailAddress进行邮件发送。1首先引入命名空间usingSystemNetMail;2将发送邮件功能封装成一个,该类中包含了发送邮件基本功能:收件人(多
1、所用工具和资源:VS2012在NETFrameword类库中提供SmtpClient(SystemNETMail)2、运行截图3、具体代码实现usingSystem;usingSystemCo
今天研究了一下利用C发邮件,很简单,贴上代码。//DefinewhatmailiwanttosendMailMessagemyMailnewMailMessage();myMailToAdd(te