欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#异步发送邮件的类

程序员文章站 2023-12-22 19:08:34
首先要定义一个邮件信息的基类,如下所示:复制代码 代码如下:/// /// base message class used for email...

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

复制代码 代码如下:

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

上一篇:

下一篇: