前些日子开发程序,我感觉也有参考价值的,是关于。net C#发送SMTP邮件的类。
我做了一个自定义的类,它继承于MailMessage类,具体如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
using System.IO;
namespace NetMailDll
{
/// <summary>
/// .net 开发SMTP服务器邮件发送
/// </summary>
public class NetMail : MailMessage
{
#region 定义私有成员和属性
/// <summary>
/// 发送邮件对象
/// </summary>
SmtpClient clientSMTP = null;
/// <summary>
/// 用于 SMTP 事务的主机的名称或 IP 地址
/// </summary>
private string SmtpServer = "";
/// <summary>
/// 用于 SMTP 事务的主机的名称或 IP 地址
/// </summary>
public string SMTPhost
{
get
{
return SmtpServer;
}
set
{
SmtpServer = value;
}
}
#endregion
#region 定义构造函数
/// <summary>
/// 创建发送邮件的类
/// </summary>
/// <param >用于 SMTP 事务的主机的名称或 IP 地址</param>
public NetMail(string SMTPhost)
{
SmtpServer = SMTPhost;
clientSMTP = new SmtpClient(SmtpServer);
}
/// <summary>
/// 创建发送邮件的类
/// </summary>
public NetMail()
{
}
#endregion
/// <summary>
/// 添加收件人的邮件地址
/// </summary>
/// <param >收件人的邮件地址</param>
/// <param >收件人的显示名称</param>
public void AddSendMailAddressTo(string address, string displayName)
{
To.Add(new MailAddress(address, displayName));
}
/// <summary>
/// 添加收件人的邮件地址
/// </summary>
/// <param >收件人的邮件地址</param>
public void AddSendMailAddressTo(string address)
{
To.Add(new MailAddress(address));
}
/// <summary>
/// 发件人的邮件地址
/// </summary>
/// <param >发件人的邮件地址</param>
/// <param >显示名称</param>
public void MailAddressFrom(string address, string displayName)
{
From = new MailAddress(address, displayName);
}
/// <summary>
/// 发件人的邮件地址
/// </summary>
/// <param >发件人的邮件地址</param>
public void MailAddressFrom(string address)
{
From = new MailAddress(address);
}
/// <summary>
/// 执行发送邮件
/// </summary>
/// <param >邮件标题</param>
/// <param >邮件正文</param>
/// <param >发送邮件的文件(包括路径和文件扩展名),没有附件则只null</param>
public void RunSendMail(string mailSubject, string mailBody, string attachmentFileName)
{
if (attachmentFileName !=""&&attachmentFileName !=null &&!System.IO.File.Exists(attachmentFileName))
{
throw new Exception("文件:" + attachmentFileName+" 不存在!");
}
if (SmtpServer == "" || SmtpServer == null)
throw new Exception("没有设置SMTP邮件服务器的名称或IP地址!");
if (From == null)
throw new Exception("没有设置发送人的邮件地址!");
if (To.Count ==0)
throw new Exception("没有设置收件人的邮件地址列表!");
try
{
Subject = mailSubject;
Body = mailBody;
Attachment Attachmentdata = null;
if (attachmentFileName != "" && attachmentFileName != null)
{
// 创建邮件附加对象,二进制文件
Attachmentdata = new Attachment(attachmentFileName, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = Attachmentdata.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(attachmentFileName);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(attachmentFileName);
disposition.ReadDate = System.IO.File.GetLastAccessTime(attachmentFileName);
// 向邮件添加附件文件
Attachments.Add(Attachmentdata);
}
if (clientSMTP == null)
clientSMTP = new SmtpClient(SmtpServer);
//Add credentials if the SMTP server requires them.
//外部邮箱可以加登陆验证
//NetworkCredential myCred = new NetworkCredential("lisontu","******");
//client.Credentials = myCred;
//client.Port = 25;
clientSMTP.Credentials = CredentialCache.DefaultNetworkCredentials;
clientSMTP.Send(this);
//释放邮件附件对象
if (Attachmentdata != null)
Attachmentdata.Dispose();
}
catch (Exception ex )
{
throw new Exception(ex.Message);
}
}
}
}