/// <summary>
/// Email工具类
/// </summary>
public class MailUtils
{
#region Fields
private string _userName;
private string _userPassword;
private string _host;
private int _port;
private SmtpClient smtp = null;
#endregion
#region Constructors
/// <summary>
/// 构造函数
/// </summary>
/// <param name="host">服务器地址(IP或域名)</param>
/// <param name="userName">认证用户名</param>
/// <param name="userPassword">认证密码</param>
public MailUtils(string host, string userName, string userPassword)
{
smtp = new SmtpClient(host);
Initialize(userName, userPassword);
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="host">服务器地址(IP或域名)</param>
/// <param name="port">服务侦听端口</param>
/// <param name="userName">认证用户名</param>
/// <param name="userPassword">认证密码</param>
public MailUtils(string host, int port, string userName, string userPassword)
{
smtp = new SmtpClient(host, port);
Initialize(userName, userPassword);
}
#endregion
#region Properties
/// <summary>
/// 认证用户名
/// </summary>
public string UserName
{
get { return _userName; }
set { _userName = value; }
}
/// <summary>
/// 认证密码
/// </summary>
public string UserPassword
{
get { return _userPassword; }
set { _userPassword = value; }
}
/// <summary>
/// 服务器地址(IP或域名)
/// </summary>
public string Host
{
get { return _host; }
set { _host = value; }
}
/// <summary>
/// 服务侦听端口
/// </summary>
public int Port
{
get { return _port; }
set { _port = value; }
}
#endregion
#region Instance Methods
/// <summary>
/// 初始化SMTP对象
/// </summary>
/// <param name="userName">认证用户名</param>
/// <param name="userPassword">认证密码</param>
public void Initialize(string userName, string userPassword)
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(userName, userPassword);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
UserName = userName;
UserPassword = userPassword;
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="strReceiver">收件人地址</param>
/// <param name="strTitle">邮件标题</param>
/// <param name="strContent">邮件内容</param>
/// <returns></returns>
public bool Send(string strReceiver, string strTitle, string strContent)
{
return Send(strReceiver, strTitle, strContent, null);
}
/// <summary>
/// 发送邮件(可带附件)
/// </summary>
/// <param name="strReceiver">收件人地址</param>
/// <param name="strSubject">邮件主题</param>
/// <param name="strContent">邮件内容</param>
/// <param name="strAttachmentPath">附件的物理地址</param>
/// <returns></returns>
public bool Send(string strReceiver, string strSubject, string strContent, string strAttachmentPath)
{
try
{
MailMessage message = new MailMessage(UserName, strReceiver, strSubject, strContent);
message.BodyEncoding = System.Text.Encoding.UTF8;
if (strAttachmentPath != null && File.Exists(strAttachmentPath))
{
message.Attachments.Add(new Attachment(strAttachmentPath));
}
message.IsBodyHtml = true;
smtp.Send(message);
return true;
}
catch
{
return false;
}
}
#endregion
#region Static Methods
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="server">stmp服务器地址</param>
/// <param name="user">用户名,包括@和域名的,这个要注意</param>
/// <param name="pwd">密码</param>
/// <param name="receiver">收件人地址</param>
/// <param name="title">邮件标题</param>
/// <param name="content">邮件内容</param>
/// <returns></returns>
public static bool Send(string server, string user, string pwd, string receiver, string title, string content)
{
return Send(server, user, pwd, receiver, title, content, null);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="server">stmp服务器地址</param>
/// <param name="user">用户名,包括@和域名的,这个要注意</param>
/// <param name="pwd">密码</param>
/// <param name="receiver">收件人地址</param>
/// <param name="title">邮件标题</param>
/// <param name="content">邮件内容</param>
/// <param name="attachmentPath">附件的绝对路径</param>
/// <returns></returns>
public static bool Send(string server, string user, string pwd, string receiver, string title, string content, string attachmentPath)
{
try
{
SmtpClient smtp = new SmtpClient(server);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(user, pwd);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage message = new MailMessage(user, receiver, title, content);
message.BodyEncoding = System.Text.Encoding.UTF8;
if (attachmentPath != null && System.IO.File.Exists(attachmentPath))
{
message.Attachments.Add(new Attachment(attachmentPath));
}
message.IsBodyHtml = true;
smtp.Send(message);
return true;
}
catch
{
return false;
}
}
#endregion
}