using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace SSCMS.CommonClass
{
/// <summary>
/// 描述:邮件操作类
/// 作者:slickly
/// 日期:2009-7-31
/// </summary>
public class MailHelper
{
protected int emailPriority = 1; //邮件紧急程度
protected string mailFormat = "text"; //邮件类型
protected string fromMail = ConfigHelper.GetConfigString("FromMail"); //发件人邮箱地址
protected string toMail; //收件人地址
protected string subject; //邮件主题
protected string body; //邮件内容
protected Encoding mailEncoding = Encoding.GetEncoding(936); //邮件编码
protected int isLocalSmtp=2; //是否启用本地smtp服务器
protected string smtpServer = ConfigHelper.GetConfigString("SmptServer"); // 远程stmp服务器名称
protected int smtpPort = ConfigHelper.GetConfigInt("SmtpPort"); // 远程stmp服务器端口
protected int isSmtpAuthenticate=1; //是否需要验证
protected bool isSSL = false; //是否需要SSL认证
protected string mailUserName = ConfigHelper.GetConfigString("MailUserName"); //用户名
protected string mailPassWord = ConfigHelper.GetConfigString("MailPassWord"); //密码
#region 属性
/// <summary>
/// 邮件紧急程度,0为低,1为普通,2为高
/// </summary>
public int EmailPriority
{
get { return emailPriority; }
set { emailPriority = value; }
}
/// <summary>
/// 邮件类型 text为纯文本型,html为Html编码型
/// </summary>
public string MailFormat
{
get { return mailFormat; }
set { mailFormat = value; }
}
/// <summary>
/// 发件人邮箱地址
/// </summary>
public string FromMail
{
get { return fromMail; }
set { fromMail = value; }
}
/// <summary>
/// 收件人地址
/// </summary>
public string ToMail
{
get { return toMail; }
set { toMail = value; }
}
/// <summary>
/// 邮件主题
/// </summary>
public string Subject
{
get { return subject; }
set { subject = value; }
}
/// <summary>
/// 邮件内容
/// </summary>
public string Body
{
get { return body; }
set { body = value; }
}
/// <summary>
/// 邮件编码格式
/// </summary>
public Encoding MailEncoding
{
get { return mailEncoding; }
set { mailEncoding = value; }
}
/// <summary>
/// 是否启用本地smtp服务器,1为启用本地,2为启用远程smtp,默认为2
/// </summary>
public int IsLocalSmtp
{
get{return isLocalSmtp;}
set{isLocalSmtp=value;}
}
/// <summary>
/// 远程stmp服务器名称
/// </summary>
public string SmtpServer
{
get { return smtpServer; }
set { smtpServer = value; }
}
/// <summary>
/// 远程stmp服务器端口号
/// </summary>
public int SmtpPort
{
get { return smtpPort; }
set { smtpPort = value; }
}
/// <summary>
/// 是否需要验证 0为不验证,1为BASIC验证,2为NTLM验证方式
/// </summary>
public int IsSmtpAuthenticate
{
get { return isSmtpAuthenticate; }
set { isSmtpAuthenticate = value; }
}
/// <summary>
/// 是否需要SSL认证
/// </summary>
public bool IsSSL
{
get { return isSSL; }
set { isSSL = value; }
}
/// <summary>
/// 邮箱用户名
/// </summary>
public string MailUserName
{
get { return mailUserName; }
set { mailUserName = value; }
}
/// <summary>
/// 邮箱密码
/// </summary>
public string MailPassWord
{
get { return mailPassWord; }
set { mailPassWord = value; }
}
#endregion
public string SendEmail()
{
try
{
MailMessage msg = new MailMessage(fromMail, toMail, subject, body);
msg.BodyEncoding = mailEncoding;
msg.SubjectEncoding = mailEncoding;
//是否启用html
switch (mailFormat)
{
case "html":
msg.IsBodyHtml = true;
break;
case "text":
msg.IsBodyHtml = false;
break;
default:
msg.IsBodyHtml = true;
break;
}
switch (emailPriority)
{
case 2:
msg.Priority = MailPriority.High;
break;
case 1:
msg.Priority = MailPriority.Normal;
break;
case 0:
msg.Priority = MailPriority.Low;
break;
}
SmtpClient SC = new SmtpClient(smtpServer, smtpPort);
SC.UseDefaultCredentials = true;
SC.Credentials = new NetworkCredential(mailUserName, mailPassWord);
SC.DeliveryMethod = SmtpDeliveryMethod.Network;
SC.EnableSsl = isSSL;
SC.Send(msg);
return "邮件发送成功!";
}
catch (Exception ex)
{
return ex.Message;
}
}
}
}