Created
July 11, 2013 13:33
-
-
Save rabintang/5975472 to your computer and use it in GitHub Desktop.
C#的Email邮件发送系统,简单易用
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*****************************************************************/ | |
* 功能:发送自定义电子邮件,可以用于在系统发送自定义系统邮件。 | |
/*****************************************************************/ | |
using System; | |
using System.Net; | |
using System.Net.Mail; | |
using System.Net.Mime; | |
namespace XFish.Utils | |
{ | |
/// <summary> | |
/// 电子邮件发送器类 | |
/// </summary> | |
public class EMailTransmitter | |
{ | |
#region 类内部变量 | |
private MailMessage _MailMessage; | |
private SmtpClient _SmtpClient; | |
private string _Password; | |
#endregion | |
#region 类属性 | |
/// <summary> | |
/// 邮件正文 | |
/// </summary> | |
public string Body | |
{ | |
get { return _MailMessage.Body; } | |
set { _MailMessage.Body = value; } | |
} | |
/// <summary> | |
/// 邮件标题 | |
/// </summary> | |
public string Title | |
{ | |
get { return _MailMessage.Subject; } | |
set { _MailMessage.Subject = value; } | |
} | |
/// <summary> | |
/// 发送密码 | |
/// </summary> | |
public string Password | |
{ | |
get { return _Password; } | |
set { _Password = value; } | |
} | |
#endregion | |
#region 类初始化 | |
/// <summary> | |
/// 处审核后类的实例 | |
/// </summary> | |
/// <param name="To">收件人地址</param> | |
/// <param name="From">发件人地址</param> | |
/// <param name="Body">邮件正文</param> | |
/// <param name="Title">邮件的主题</param> | |
/// <param name="Password">发件人密码</param> | |
public EMailTransmitter(string to, string from, string body, string title, string password) | |
{ | |
_MailMessage = new MailMessage(); | |
_MailMessage.To.Add(to); | |
_MailMessage.From = new System.Net.Mail.MailAddress(from); | |
_MailMessage.Subject = title; | |
_MailMessage.Body = body; | |
_MailMessage.IsBodyHtml = true; | |
_MailMessage.BodyEncoding = System.Text.Encoding.UTF8; | |
_MailMessage.Priority = System.Net.Mail.MailPriority.Normal; | |
_Password = password; | |
} | |
#endregion | |
#region 类方法 | |
/// <summary> | |
/// 增加接收方地址 | |
/// </summary> | |
/// <param name="to">接收方的邮件地址</param> | |
public void AddReceiver(string to) | |
{ | |
MailAddress address = new MailAddress(to); | |
if (!_MailMessage.To.Contains(address)) | |
_MailMessage.To.Add(to); | |
} | |
/// <summary> | |
/// 删除指定的接收方地址 | |
/// </summary> | |
/// <param name="to">接收方的邮件地址</param> | |
public void RemoveReceiver(string to) | |
{ | |
MailAddress address = new MailAddress(to); | |
if (_MailMessage.To.Contains(address)) | |
_MailMessage.To.Remove(address); | |
} | |
/// <summary> | |
/// 添加附件 | |
/// </summary> | |
/// <param name="Path">附件的路径</param> | |
public void AddAttachments(string path) | |
{ | |
string[] paths = path.Split(','); | |
Attachment data; | |
ContentDisposition disposition; | |
for (int i = 0; i < paths.Length; i++) | |
{ | |
data = new Attachment(paths[i], MediaTypeNames.Application.Octet);//实例化附件 | |
disposition = data.ContentDisposition; | |
disposition.CreationDate = System.IO.File.GetCreationTime(paths[i]);//获取 附件的创建日期 | |
disposition.ModificationDate = System.IO.File.GetLastWriteTime(paths[i]);//获取附件的修改日期 | |
disposition.ReadDate = System.IO.File.GetLastAccessTime(paths[i]);//获取附件的读取日期 | |
_MailMessage.Attachments.Add(data);//添加到附件中 | |
} | |
} | |
/// <summary> | |
/// 删除附件 | |
/// </summary> | |
/// <param name="path">附件的路径</param> | |
public void RemoveAttachments(string path) | |
{ | |
string[] paths = path.Split(','); | |
Attachment data; | |
ContentDisposition disposition; | |
for (int i = 0; i < paths.Length; i++) | |
{ | |
data = new Attachment(paths[i], MediaTypeNames.Application.Octet); | |
disposition = data.ContentDisposition; | |
disposition.CreationDate = System.IO.File.GetCreationTime(paths[i]);//获取 附件的创建日期 | |
disposition.ModificationDate = System.IO.File.GetLastWriteTime(paths[i]);//获取附件的修改日期 | |
disposition.ReadDate = System.IO.File.GetLastAccessTime(paths[i]);//获取附件的读取日期 | |
if(_MailMessage.Attachments.Contains(data)) | |
_MailMessage.Attachments.Remove(data); | |
} | |
} | |
/// <summary> | |
/// 异步发送邮件 | |
/// </summary> | |
/// <param name="CompletedMethod">完成后调用的事件</param> | |
public void SendAsync(SendCompletedEventHandler CompletedMethod) | |
{ | |
if (_MailMessage != null) | |
{ | |
_SmtpClient = new SmtpClient(); | |
_SmtpClient.Credentials = new System.Net.NetworkCredential(_MailMessage.From.Address, _Password);//设置发件人身份的票据 | |
_SmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; | |
_SmtpClient.Host = "smtp." + _MailMessage.From.Host; | |
_SmtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//注册异步发送邮件完成时的事件 | |
_SmtpClient.SendAsync(_MailMessage, _MailMessage.Body); | |
} | |
} | |
/// <summary> | |
/// 发送邮件 | |
/// </summary> | |
public void Send() | |
{ | |
if (_MailMessage != null) | |
{ | |
_SmtpClient = new SmtpClient(); | |
_SmtpClient.Credentials = new System.Net.NetworkCredential(_MailMessage.From.Address, _Password);//设置发件人身份的票据 | |
_SmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; | |
_SmtpClient.Host = "smtp." + _MailMessage.From.Host; | |
_SmtpClient.Send(_MailMessage); | |
} | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment