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
}

(0)

相关推荐

  • c#调用qq邮箱smtp发送邮件修改版代码分享

    复制代码 代码如下: try            {                MailMessage mm = new MailMessage();                MailAddress Fromma = new MailAddress("xxxx@qq.com");                MailAddress Toma = new MailAddress("MMMMMMM@qq.com", null);              

  • C#编程实现发送邮件的方法(可添加附件)

    本文实例讲述了C#编程实现发送邮件的方法.分享给大家供大家参考,具体如下: using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Mail; namespace WindowsFo

  • C#实现发送邮件的三种方法

    本文实例讲述了C#实现发送邮件的三种方法.分享给大家供大家参考.具体方法分析如下: 一.问题: 最近公司由于一个R&I项目的需要,用户要求在购买产品或出货等一些环节,需要发送邮件提醒或者说每周一让系统自动采集数据发送一封E-mail,因此我也就找来相关资料,写了一个Demo分享给大家,大家共同学习学习. 二.实现代码: 通过.Net FrameWork 2.0下提供的"System.Net.Mail"可以轻松的实现,本文列举了3种途径来发送: 1.通过Localhost: 2.

  • c#实现服务器性能监控并发送邮件保存日志

    客户端代码 复制代码 代码如下: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.ServiceProcess;using System.Text;using System.Threading;using System.Management;using System.Configurat

  • c# SendMail发送邮件实例代码

    复制代码 代码如下: using System;using System.Collections.Generic;using System.Net;using System.Net.Mail;using System.Text; namespace Common{    /// <summary>    /// 基于system.net.mail发送邮件,支持附件    /// </summary>    public class NetSendMail    {        p

  • C#.NET发送邮件的实例代码

    复制代码 代码如下: using System;using System.Collections.Generic;using System.Text;using System.Net.Mail;using System.Net;namespace MyQuery.Utils{    /// <summary>    /// 封装邮件处理    /// by 贾世义 2011-6-3    /// </summary>    public static class MailHelpe

  • C#中发送邮件代码

    始找的代码只能发送无SMTP验证的邮件,但现在很多EMAIL发送时都需要验证,后来查找了下MSDN的帮助,找到了发送验证的代码,贴出来希望对大家有所帮助! 复制代码 代码如下: public static int sendmail(string to, string body,string subject) { try { int nContain = 0; ///添加发件人地址 string from = "你的发送EMAIL"; MailMessage mailMsg = new

  • c#利用webmail邮件系统发送邮件示例分享

    在C#中发送邮件的方式有2种,一种是使用webmail方式进行发送,另外一种就是采用netmail发送的方式,在采用这2种方式发送邮件时,如果采用公用的邮件服务器(如126邮件服务器,Sina的邮件服务器)都是需要授权认证才能够发送,如果是采用Gmail的话,还会有每天发送邮件的数量等限制.这2种方式是经过我测试通过了的代码,只需要将邮件的用户名和密码修改成自己的即可,同时也可以修改邮件服务器,改成自己配置的邮件服务器. 复制代码 代码如下: /// <summary>    /// 发送Em

  • C#实现发送邮件的方法

    本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下 #region 发送邮件部分 private static String fromMail = "1111@126.com"; ///邮箱名称 private static String mailPwd = "111111"; ///密码 private static string toMail = "2222@163.com"; ///邮箱服务器 privat

  • c#使用netmail方式发送邮件示例

    复制代码 代码如下: /// <summary>    /// NetMail方式测试通过    /// </summary>    private void TestSend()    {        System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();        //收件人地址        mm.To.Add(new System.Net.Mail.MailAddress("xx

随机推荐