| EMAIL发送系统(C#+基于SMTP认证)[转] |
|
| EMAIL发送系统(C#+基于SMTP认证)[转] |
|
|
|
作者:佚名 文章来源:不详 点击数: 更新时间:2006-12-6 1:06:56  |
EMAIL发送系统(C#+基于SMTP认证)[转] EMAIL发送系统(C#+基于SMTP认证)[转] 在为公司写通知服务时,从网上找到了以上地址,非常感谢原作者创造性的劳动。改写的目的是为了适应作为服务运行的要求: 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 /// /// 邮件可以通过 Microsoft Windows 2000 中内置的 SMTP 邮件服务或任意 SMTP 服务器来传送 /// public class SmtpMail { private const string ENTER="\r\n"; /// /// 设定语言代码,默认设定为GB2312,如不需要可设置为"" /// private string m_charset="GB2312"; /// /// 服务器交互记录 /// private StringBuilder m_logs = new StringBuilder(); private string m_ErrCode; /// /// SMTP错误代码哈希表 /// private Hashtable m_ErrCodeHT = new Hashtable(); /// /// SMTP正确代码哈希表 /// private Hashtable m_RightCodeHT = new Hashtable(); /// /// 最多收件人数量 /// private int m_recipientMaxnum = 2; /// /// 重复时间,以秒为单位 /// private int m_RepeatTime = 120; /// /// 服务器出错或拒绝后的等待时间,以毫秒为单位 /// private int m_WaitTime = 20000; /// /// 初始化 类的新实例 /// public SmtpMail() { SMTPCodeAdd(); } #region Properties 定义属性 /// /// 服务器交互记录,如发现本组件不能使用的SMTP服务器,请将出错时的Logs发给我(lion-a@sohu.com),我将尽快查明原因。 /// public string Logs { get { return m_logs.ToString(); } } /// /// 最多收件人数量 /// public int RecipientMaxNum { set { m_recipientMaxnum = value; } get { return m_recipientMaxnum; } } /// /// 设定语言代码,默认设定为GB2312,如不需要可设置为"" /// public string Charset { get { return this.m_charset; } set { this.m_charset = value; } } /// /// 重复时间,以秒为单位 /// public int RepeatTime { get {return m_RepeatTime;} set {m_RepeatTime = value;} } /// /// 服务器出错或拒绝后的等待时间,以毫秒为单位 /// public int WaitTime { get {return m_WaitTime;} set {m_WaitTime = value > 10000?value:10000;} } #endregion #region Methods 定义方法 /// /// 邮件服务器域名和验证信息 /// 形如:"user:pass@www.server.com:25",也可省略次要信息。如"user:pass@www.server.com"或"www.server.com" /// /// 输入用户名、密码、邮件服务器域名、端口号 /// 返回邮件服务器域名 /// 返回用户名 /// 返回密码 /// 返回端口号 /// 返回是否需要SMTP验证 /// 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; } /// /// 添加邮件附件 /// /// 附件绝对路径 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 { if(File.Exists(filePath[i])) { m_A[1] [2] [3] [4] [5] 下一页
|
| 文章录入:wuyongjian 责任编辑:wuyongjian |
|
上一篇文章: 自定义组件之属性Property与Attribute 下一篇文章: 读取非本站目录的其他文件进行下载的例子 |
| 【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |