当前位置:  编程技术>.net/c#/asp.net

c#异步发送邮件的类实例代码

    来源: 互联网  发布时间:2014-08-30

    本文导语:  一个使用c#异步发送邮件的类。   首先,定义一个邮件信息的基类。   代码示例: /// /// Base message class used for emails /// public class Message { #region Constructor /// /// Constructor /// public Message() { } #endregion #region Properties /// ...

一个使用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
}

    
 
 

您可能感兴趣的文章:

  • c#异步task示例分享(异步操作)
  • 推荐代码:c# 异步更新UI 不阻塞 流畅
  • C#同步和异步调用方法实例
  • c#异步读取数据库与异步更新ui的代码实现
  • C#异步调用的好处和方法分享
  • c#异步操作后台运行(backgroundworker类)示例
  • C#基础之异步调用实例教程
  • 解析C#中委托的同步调用与异步调用(实例详解)
  • C# 委托的三种调用示例(同步调用 异步调用 异步回调)
  • c#实现简单控制台udp异步通信程序示例
  • C#同步、异步远程下载文件实例
  • c# 异步调用的代码(简单示例)
  • 深入分析C#异步编程详解
  • c#异步发送邮件的类
  • c#并行任务多种优化方案分享(异步委托)
  • c#(Socket)异步套接字代码示例
  • C# Socket 异步套接字的代码一例
  • jquery异步请求并改变页面内容的实例分析
  • jquery的ajax异步请求接收返回json数据实例
  • PHP异步调用socket与php异步执行实例
  • .net4.5使用async和await异步编程实例
  • Android 异步加载图片的实例代码
  • Jquery Ajax解析XML数据(同步及异步调用)简单实例
  • php+js实现异步图片上传实例分享
  • PHP异步执行实例解析
  • Jquery.Form 异步提交表单的简单实例
  • .NET中的async和await关键字使用及Task异步调用实例
  • 使用异步方式调用同步方法(实例详解)
  • Cocos2d-x 3.0多线程异步加载资源实例
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • struts+spring+hibernate+jquery实现分页功能的几个基本类介绍(异步加载)
  • Linux 中关于异步函数的问题
  • java异步方法调用框架 asyn4j
  • 异步消息传递框架 Errai
  • Balsa 异步电路仿真和合成系统
  • 异步I/O库 libeio
  • 异步 JS 工具 Async.js
  • 异步JS开发库 Wind.js
  • 异步JS开发库 Jscex
  • JS异步编程库 WinJS
  • 异步connect的问题,27日18:00前结账。
  • ACE linux下的异步IO
  • unix下异步进程的问题
  • 请问单线程异步机制的优势在哪里?
  • Node.js 异步错误处理 LAEH2
  • 纯异步的Server简单实现 Server
  • 内核进程间发送信号用哪个函数,如果没有这东西,那内核里异步通知用哪个东西
  • PHP异步执行技巧分享
  • Swift 异步编程库 Wyrd
  • 异步网络编程框架 itachi
  • C++异步网络开发库 ez_poll


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3