结合网络上一些邮件发送的功能,特整理了这篇类库。懒羊
/**/
///名称:邮件发送类
///开发时间:2008-01-18
///开发人:杨剑(anchor)
///http://www.swordyang.cn
///sn1985boy@sohu.com
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace cn.SwordYang
... {
public class MailSender
...{
protected int emailPriority=1; //邮件紧急程度
protected string mailFormat="text"; //邮件类型
protected string fromMail; //发件人邮箱地址
protected string toMail; //收件人地址
protected string subject; //邮件主题
protected string body; //邮件内容
protected Encoding mailEncoding=Encoding.GetEncoding(936); //邮件编码
//protected int isLocalSmtp=2; //是否启用本地smtp服务器
protected string smtpServer;
protected int smtpPort=25;
//protected int isSmtpAuthenticate=1; //是否需要验证
protected bool isSSL=true; //是否需要SSL认证
protected string mailUserName; //用户名
protected string 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);
msg.Subject = subject;
msg.Body = body;
msg.BodyEncoding =mailEncoding;
//是否启用html
switch (mailFormat)
...{
case "html":
msg.IsBodyHtml=true;
break;
case "text":
msg.IsBodyHtml=false;
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);
if (!String.IsNullOrEmpty(mailUserName) && !String.IsNullOrEmpty(mailPassWord))
...{
SC.Credentials = new NetworkCredential(mailUserName, mailPassWord);
}
SC.EnableSsl=isSSL;
SC.Send(msg);
return "Send Mail OK";
/**//* 原system.web.mail
switch (mailPriority)
{
case 2:
msg.Priority = MailPriority.High;
break;
case 1:
msg.Priority = MailPriority.Normal;
break;
case 0:
msg.Priority = MailPriority.Low;
break;
}
switch (mailFormat)
{
case "html":
msg.BodyFormat = MailFormat.Html;
break;
case "text":
msg.BodyFormat = MailFormat.Text;
break;
}
if (isLocalSmtp==2)
{
//1 代表使用 local smtp, 2 外部 smtp
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", isLocalSmtp);
SmtpMail.SmtpServer = smtpServer;
//SMTP Server
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
if (SmtpPort != "")
{
//Server port, gmail is 465
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", SmtpPort);
}
//Authentication method, ssl or not, Username and password for the SMTP Server
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",isSmtpAuthenticate);
//cdoBasic 基本验证 gmail使用ssl验证
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", isSSL);
//账号
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mailUserName);
//密码
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", mailPassWord);
}
SmtpMail.Send(msg);
*/
}
catch (Exception ex)
...{
return ex.Message;
}
}
}
}
///开发时间:2008-01-18
///开发人:杨剑(anchor)
///http://www.swordyang.cn
///sn1985boy@sohu.com
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace cn.SwordYang
... {
public class MailSender
...{
protected int emailPriority=1; //邮件紧急程度
protected string mailFormat="text"; //邮件类型
protected string fromMail; //发件人邮箱地址
protected string toMail; //收件人地址
protected string subject; //邮件主题
protected string body; //邮件内容
protected Encoding mailEncoding=Encoding.GetEncoding(936); //邮件编码
//protected int isLocalSmtp=2; //是否启用本地smtp服务器
protected string smtpServer;
protected int smtpPort=25;
//protected int isSmtpAuthenticate=1; //是否需要验证
protected bool isSSL=true; //是否需要SSL认证
protected string mailUserName; //用户名
protected string 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);
msg.Subject = subject;
msg.Body = body;
msg.BodyEncoding =mailEncoding;
//是否启用html
switch (mailFormat)
...{
case "html":
msg.IsBodyHtml=true;
break;
case "text":
msg.IsBodyHtml=false;
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);
if (!String.IsNullOrEmpty(mailUserName) && !String.IsNullOrEmpty(mailPassWord))
...{
SC.Credentials = new NetworkCredential(mailUserName, mailPassWord);
}
SC.EnableSsl=isSSL;
SC.Send(msg);
return "Send Mail OK";
/**//* 原system.web.mail
switch (mailPriority)
{
case 2:
msg.Priority = MailPriority.High;
break;
case 1:
msg.Priority = MailPriority.Normal;
break;
case 0:
msg.Priority = MailPriority.Low;
break;
}
switch (mailFormat)
{
case "html":
msg.BodyFormat = MailFormat.Html;
break;
case "text":
msg.BodyFormat = MailFormat.Text;
break;
}
if (isLocalSmtp==2)
{
//1 代表使用 local smtp, 2 外部 smtp
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", isLocalSmtp);
SmtpMail.SmtpServer = smtpServer;
//SMTP Server
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
if (SmtpPort != "")
{
//Server port, gmail is 465
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", SmtpPort);
}
//Authentication method, ssl or not, Username and password for the SMTP Server
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",isSmtpAuthenticate);
//cdoBasic 基本验证 gmail使用ssl验证
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", isSSL);
//账号
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mailUserName);
//密码
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", mailPassWord);
}
SmtpMail.Send(msg);
*/
}
catch (Exception ex)
...{
return ex.Message;
}
}
}
}