• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > c#异步发送邮件的类

c#异步发送邮件的类

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c#异步编程,c#异步,c#异步委托,c#异步线程,c#异步回调等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

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

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

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

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#使用系统方法发送异步邮件完整实例
  • C#下载歌词文件的同步和异步方法
  • C#异步委托调用实例分析
  • C#实现异步连接Sql Server数据库的方法
  • C#实现异步发送邮件的方法
  • C#基础之异步调用实例教程
  • c#异步发送邮件的类
  • c#并行任务多种优化方案分享(异步委托)
  • c#异步读取数据库与异步更新ui的代码实现
  • C#同步和异步调用方法实例

相关文章

  • 2017-05-28基于XSLT调试的相关问题
  • 2017-05-28C#一个简单的定时小程序实现代码
  • 2017-05-28深入解析:打造自动消失的对话框
  • 2017-05-28c#程序定期把内存信息记录到log日志示例
  • 2017-05-28C#编程自学之数据类型和变量三
  • 2017-05-28C#中用foreach语句遍历数组及将数组作为参数的用法
  • 2017-05-28深入HTTP head的使用详解
  • 2017-05-28C#常见应用函数实例小结
  • 2017-05-28基于C#中XmlWriter写入Xml的深入分析
  • 2017-05-28.net4.5使用async和await异步编程实例

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#序列化成XML注意细节
    • 使用C#来编写一个异步的Socket服务器
    • C#使用Region对图形区域构造和填充的方法
    • C#获得MAC地址(网卡序列号)的实现代码
    • C# 设计模式系列教程-装饰模式
    • 解析Silverlight调用WCF/Rest异常的解决方法
    • WinForm项目开发中Excel用法实例解析
    • .NET中的async和await关键字使用及Task异步调用实例
    • C#列出所有物理网络适配器的方法
    • C# Linq读取XML文件的实例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有