这个是对于
[原创]EMAIL发送系统(C#+基于SMTP认证)
http://www.lionsky.net/MyWebsite/article/list.aspx?id=430
的改版
在为公司写通知服务时,从网上找到了以上地址,非常感谢原作者创造性的劳动。改写的目的是为了适应作为服务运行的要求:
1、适应多线程的要求,发送邮件服务可在后台运行,将与SMTP服务器的连接视为独占资源。
2、适应稳定性的要求,不再以简单地抛出异常来处理错误,在出现异常后等待一定时间间隔后重试,重试一段时间间隔后若还时发不出去,则认为是SMTP出错,返回发送邮件不成功的标识。
3、精简属性、方法,与邮件相关的信息不再作为属性,而是作为send的参数传入;只公布了一个无重载的send方法。以此类为基类,另写通知服务要求的接口方法。
以下是改写后的代码:
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Threading;
namespace Deep.SendEmail
{
#region AspNetPager Server Control
/// <summary>
/// 邮件可以通过 Microsoft Windows 2000 中内置的 SMTP 邮件服务或任意 SMTP 服务器来传送
/// </summary>
public class SmtpMail
{
private const string ENTER="\r\n";
/// <summary>
/// 设定语言代码,默认设定为GB2312,如不需要可设置为""
/// </summary>
private string m_charset="GB2312";
/// <summary>
/// 服务器交互记录
/// </summary>
private StringBuilder m_logs = new StringBuilder();
private string m_ErrCode;
/// <summary>
/// SMTP错误代码哈希表
/// </summary>
private Hashtable m_ErrCodeHT = new Hashtable();
/// <summary>
/// SMTP正确代码哈希表
/// </summary>
private Hashtable m_RightCodeHT = new Hashtable();
/// <summary>
/// 最多收件人数量
/// </summary>
private int m_recipientMaxnum = 2;
/// <summary>
/// 重复时间,以秒为单位
/// </summary>
private int m_RepeatTime = 120;
/// <summary>
/// 服务器出错或拒绝后的等待时间,以毫秒为单位
/// </summary>
private int m_WaitTime = 20000;
/// <summary>
/// 初始化 <see cref="Lion.Web.Mail.SmtpMail"/> 类的新实例
/// </summary>
public SmtpMail()
{
SMTPCodeAdd();
}
#region Properties 定义属性
/// <summary>
/// 服务器交互记录,如发现本组件不能使用的SMTP服务器,请将出错时的Logs发给我([email protected]),我将尽快查明原因。
/// </summary>
public string Logs
{
get
{
return m_logs.ToString();
}
}
/// <summary>
/// 最多收件人数量
/// </summary>
public int RecipientMaxNum
{
set
{
m_recipientMaxnum = value;
}
get
{
return m_recipientMaxnum;
}
}
/// <summary>
/// 设定语言代码,默认设定为GB2312,如不需要可设置为""
/// </summary>
public string Charset
{
get
{
return this.m_charset;
}
set
{
this.m_charset = value;
}
}
/// <summary>
/// 重复时间,以秒为单位
/// </summary>
public int RepeatTime
{
get {return m_RepeatTime;}
set {m_RepeatTime = value;}
}
/// <summary>
/// 服务器出错或拒绝后的等待时间,以毫秒为单位
/// </summary>
public int WaitTime
{
get {return m_WaitTime;}
set {m_WaitTime = value > 10000?value:10000;}
}
#endregion
#region Methods 定义方法
/// <summary>
/// 邮件服务器域名和验证信息
/// 形如:"user:[email protected]:25",也可省略次要信息。如"user:[email protected]"或"www.server.com"
/// </summary>
/// <param name="mailDomain">输入用户名、密码、邮件服务器域名、端口号</param>
/// <param name="mailServer">返回邮件服务器域名</param>
/// <param name="mailServerUserName">返回用户名</param>
/// <param name="password">返回密码</param>
/// <param name="mailserverport">返回端口号</param>
/// <param name="needSmtp">返回是否需要SMTP验证</param>
/// <returns></returns>
private bool SetMailDomain(string mailDomain,out string mailServer,out string mailServerUserName,out string password,
out int mailserverport,out bool needSmtp)
{
bool isRight = false;
//为输出变量赋初值
mailServer = string.Empty;
mailServerUserName = String.Empty;
password = String.Empty;
mailserverport = 25;
needSmtp = false;
mailServer = mailDomain.Trim();
int tempint;
if( mailServer != "" )
{
tempint = mailServer.IndexOf("@");
isRight = true;
if(tempint!=-1)
{
string str = mailServer.Substring(0,tempint);
mailServerUserName = str.Substring(0,str.IndexOf(":"));
password = str.Substring(str.IndexOf(":")+1,str.Length-str.IndexOf(":")-1);
needSmtp = !(password==string.Empty);
mailServer = mailDomain.Substring(tempint+1,mailDomain.Length-tempint-1);
}
tempint = mailServer.IndexOf(":");
if(tempint != -1)
{
mailserverport = System.Convert.ToInt32(mailServer.Substring(tempint+1,mailServer.Length-tempint-1));
mailServer = mailServer.Substring(0,tempint);
}
}
return isRight;
}
/// <summary>
/// 添加邮件附件
/// </summary>
/// <param name="filePath">附件绝对路径</param>
private IList AddAttachment(params string[] filePath)
{
if(filePath == null || filePath.Length == 0)
{
return null;
}
IList m_Attachments = new System.Collections.ArrayList();// 邮件附件列表
for(int i=0;i<filePath.Length;i++)
{
if(File.Exists(filePath[i]))
{
m_Attachments.Add(filePath[i]);
}
else
{
m_logs.Append("错误:没找到文件名为"+filePath[i]+"的附件文件!"+ENTER);
}
}
return m_Attachments;
}
/// <summary>
/// 添加一组收件人(不超过m_recipientMaxnum个),参数为字符串数组
/// </summary>
/// <param name="recipients">保存有收件人地址的字符串数组(不超过m_recipientMaxnum个)</param>
private Hashtable AddRecipient(params string[] recipients)
{
if(recipients==null || recipients.Length == 0)
{
return null;
}
Hashtable recipientList=new Hashtable();// 收件人列表
for(int i=0;i<recipients.Length;i++)
{
string recipient = recipients[i].Trim();
if(recipient !=String.Empty && recipient.IndexOf("@") != -1)
{
recipientList.Add(recipientList.Count,recipients[i]);
}
}
return recipientList;
}
/// <summary>
/// 发送邮件方法
/// </summary>
/// <param name="smtpServer">smtp服务器信息,如"username:[email protected]:25",也可去掉部分次要信息,如"
本文地址:http://com.8s8s.com/it/it42104.htm