当前位置: 移动技术网 > IT编程>开发语言>c# > c#异步发送邮件的类

c#异步发送邮件的类

2019年07月18日  | 移动技术网IT编程  | 我要评论
首先要定义一个邮件信息的基类,如下所示:复制代码 代码如下:/// <summary>/// 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
}

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

  • 如何使用C# 捕获进程输出

    intro很多时候我们可能会需要执行一段命令获取一个输出,遇到的比较典型的就是之前我们需要用 ffmpeg 实现视频的编码压缩水印等一系列操作,当时使用的是 f... [阅读全文]
  • 全面分析c# LINQ

    大家好,这是 [c#.net 拾遗补漏] 系列的第 08 篇文章,今天讲 c# 强大的 linq 查询。linq 是我最喜欢的 c# 语言特性之一。linq 是... [阅读全文]
  • C# DataTable常见用法汇总

    c# datatable 的常见用法:(1)新建数据表。(2)向表添加列。(3)设置表特定行与列的数据值。(4)将某行数据加入到表。(5)合并表。(6)复制表。... [阅读全文]
  • C# 如何解析获取Url参数值

    今天遇到一个需求,需要处理通过接口传过来的一个参数,参数内容为一个拼接好的url地址,且该地址还会携带了一些额外的参数,包括但不限于数字,字符串,json串。样... [阅读全文]
  • C# 实现dataGridView选中一行右键出现菜单的示例代码

    在窗体中添加datagridview控件和contextmenustrip1控件,修改datagridview属性,将contextmenustrip控件绑定d... [阅读全文]
  • WPF实现手风琴式轮播图切换效果

    WPF实现手风琴式轮播图切换效果

    本文实例为大家分享了wpf实现轮播图切换效果的具体代码,供大家参考,具体内容如下实现效果如下:步骤:1、自定义控件myimagecontrol实现图片的裁切和动... [阅读全文]
  • WPF实现3D翻牌式倒计时特效

    WPF实现3D翻牌式倒计时特效

    本文实例为大家分享了wpf实现3d翻牌式倒计时的具体代码,供大家参考,具体内容如下实现效果如下:思路:使用自定义控件,设置一个背板 mycardcontrolb... [阅读全文]
  • WPF实现平面三角形3D运动效果

    WPF实现平面三角形3D运动效果

    本文实例为大家分享了wpf实现平面三角形3d运动效果的具体代码,供大家参考,具体内容如下实现效果如下:思路:封装三角形三个顶点和路径的三角形类,图形渲染时同步更... [阅读全文]
  • WPF实现3D粒子波浪效果

    WPF实现3D粒子波浪效果

    本文实例为大家分享了wpf实现3d粒子波浪效果的具体代码,供大家参考,具体内容如下实现效果如下:步骤:1、3d粒子类particle.cs2、粒子系统parti... [阅读全文]
  • 谈谈c#中的索引器

    概念索引器(indexer) 允许类中的对象可以像数组那样方便、直观的被引用。当为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array... [阅读全文]
验证码:
移动技术网