.NET 1.1 下不使用 System.Web.Mail.SmtpMail 发送邮件的其它选择


      最近工作原因需要维护 ASP.NET 1.1 的程序,本来用着 C# 2.0 System.Net.Mail namespace 发送邮件用得好好得,但 ASP.NET 1.1 里面的 System.Web.Mail 死活问题就是多,于是选择其它策略,封装了邮件发送的接口,再以其它方式来实现邮件发送,于是就有了下面这些文字。

定义抽象接口以封装所有实现:

None.gif using  System;
None.gif
using  System.Web.Mail;
None.gif
None.gif
namespace  YywMail
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public abstract class MySmtpMail
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Fields#region Fields
InBlock.gif
InBlock.gif        
private string _defaultCharset = "GB2312";
InBlock.gif        
private int _defaultSmtpPort = 25;
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Properties#region Properties
InBlock.gif
InBlock.gif        
protected string DefaultCharset
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this._defaultCharset; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected int DefaultSmtpPort
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this._defaultSmtpPort;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Methods#region Methods
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取默认实例
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static MySmtpMail GetDefaultInstance()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 此处可通过外部配置文件定义具体实现类型,再
InBlock.gif            
// 通过 Activator.CreateInstance() 获取类型实例            
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 做一些初始化的工作
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public abstract void Open();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 销毁对象
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public abstract void Close();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 发送邮件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="message"></param>
InBlock.gif        
/// <param name="smtpServer"></param>
InBlock.gif        
/// <param name="serverUsername"></param>
InBlock.gif        
/// <param name="serverPassword"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public bool Send(MailMessage message, string smtpServer, string serverUsername, string serverPassword)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Send(message, smtpServer, serverUsername, serverPassword, this._defaultSmtpPort);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public abstract bool Send(MailMessage message, string smtpServer, string serverUsername, string serverPassword, int smtpPort);
InBlock.gif
InBlock.gif        
public static string[] GetTo(MailMessage message)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (message == null)
InBlock.gif                
throw new ArgumentNullException("message");
InBlock.gif
InBlock.gif            
if (Globals.IsNullorEmpty(message.To))
InBlock.gif                
return null;
InBlock.gif
InBlock.gif            
return message.To.Split(';');
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string[] GetCc(MailMessage message)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (message == null)
InBlock.gif                
throw new ArgumentNullException("message");
InBlock.gif
InBlock.gif            
if (Globals.IsNullorEmpty(message.Cc))
InBlock.gif                
return null;
InBlock.gif
InBlock.gif            
return message.Cc.Split(';');
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string[] GetBcc(MailMessage message)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (message == null)
InBlock.gif                
throw new ArgumentNullException("message");
InBlock.gif
InBlock.gif            
if (Globals.IsNullorEmpty(message.Bcc))
InBlock.gif                
return null;
InBlock.gif
InBlock.gif            
return message.Bcc.Split(';');
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

注: 按照常理,使用前先 Open() 一下,使用后也别忘了 Close()

实现方案一(Jmail 组件):

在 .NET 中使用 Jmail  需要如下设置:
1、安装jmail;
2、找到jmail.dll;
3、注册该组件Jmail.dll,作法是将jmail.dll文件拷贝到system32目录下,再运行命令“regsvr32 jmail.dll”(不包括引号),卸载可运行“regsvr32 /u jmail.dll”;
4、执行Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin\ildasm.exe(可使用Visual Studio .Net 2003 命令提示),
格式如下:tlbimp c:\Program Files\Dimac\w3JMail4\jmail.dll /out:MyJmail.dll /namespace:MyJmail
生成MyJmail.dll后,将它引用至项目中。

下载组件

接下来就是实现类的编写了:

None.gif using  System;
None.gif
using  System.Web.Mail;
None.gif
None.gif
namespace  YywMail
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class JMailSmtpMail : MySmtpMail
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Fields#region Fields
InBlock.gif
InBlock.gif        MyJmail.Message jmail 
= null;
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Methods#region Methods
InBlock.gif
InBlock.gif        
public override void Open()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            jmail 
= new MyJmail.Message();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override bool Send(MailMessage message, string smtpServer, string serverUsername, string serverPassword, int smtpPort)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (jmail == null)
InBlock.gif                
throw new Exception("smtp is Closed!");
InBlock.gif            
InBlock.gif            
if (message == null)
InBlock.gif                
throw new ArgumentNullException("message");
InBlock.gif
InBlock.gif            DateTime t 
= DateTime.Now;
InBlock.gif
InBlock.gif            
//Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false
InBlock.gif
            jmail.Silent = false;
InBlock.gif
InBlock.gif            
//jmail创建的日志,前提loging属性设置为true
InBlock.gif
            jmail.Logging = true;
InBlock.gif
InBlock.gif            
//字符集,缺省为"US-ASCII"
InBlock.gif
            jmail.Charset = base.DefaultCharset;
InBlock.gif
InBlock.gif            
//信件的contentype. 缺省是"text/plain") : 字符串如果你以HTML格式发送邮件, 改为"text/html"即可。
InBlock.gif
            if (message.BodyFormat == MailFormat.Html)
InBlock.gif                jmail.ContentType 
= "text/html";
InBlock.gif
InBlock.gif            
InBlock.gif            jmail.Priority 
= GetJmailPriority(message.Priority);
InBlock.gif
InBlock.gif            
//添加收件人
InBlock.gif
            string[] toArray = MySmtpMail.GetTo(message);
InBlock.gif            
if (toArray != null && toArray.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值