`
yyw84
  • 浏览: 76591 次
社区版块
存档分类
最新评论

.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 死活问题就是多,于是选择其它策略,封装了邮件发送的接口,再以其它方式来实现邮件发送,于是就有了下面这些文字。

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

using System;
using System.Web.Mail;

namespace YywMail
{
    
public abstract class MySmtpMail
    
{
        
Fields#region Fields

        
private string _defaultCharset = "GB2312";
        
private int _defaultSmtpPort = 25;

        
#endregion

        
        
Properties#region Properties

        
protected string DefaultCharset
        
{
            
get return this._defaultCharset; }
        }


        
protected int DefaultSmtpPort
        
{
            
get return this._defaultSmtpPort;}
        }


        
#endregion


        
Methods#region Methods

        
/**//// <summary>
        
/// 获取默认实例
        
/// </summary>
        
/// <returns></returns>
        public static MySmtpMail GetDefaultInstance()
        
{
            
// 此处可通过外部配置文件定义具体实现类型,再
            
// 通过 Activator.CreateInstance() 获取类型实例            
        }


        
/**//// <summary>
        
/// 做一些初始化的工作
        
/// </summary>
        public abstract void Open();

        
/**//// <summary>
        
/// 销毁对象
        
/// </summary>
        public abstract void Close();

        
/**//// <summary>
        
/// 发送邮件
        
/// </summary>
        
/// <param name="message"></param>
        
/// <param name="smtpServer"></param>
        
/// <param name="serverUsername"></param>
        
/// <param name="serverPassword"></param>
        
/// <returns></returns>
        public bool Send(MailMessage message, string smtpServer, string serverUsername, string serverPassword)
        
{
            
return Send(message, smtpServer, serverUsername, serverPassword, this._defaultSmtpPort);
        }


        
public abstract bool Send(MailMessage message, string smtpServer, string serverUsername, string serverPassword, int smtpPort);

        
public static string[] GetTo(MailMessage message)
        
{
            
if (message == null)
                
throw new ArgumentNullException("message");

            
if (Globals.IsNullorEmpty(message.To))
                
return null;

            
return message.To.Split(';');
        }


        
public static string[] GetCc(MailMessage message)
        
{
            
if (message == null)
                
throw new ArgumentNullException("message");

            
if (Globals.IsNullorEmpty(message.Cc))
                
return null;

            
return message.Cc.Split(';');
        }


        
public static string[] GetBcc(MailMessage message)
        
{
            
if (message == null)
                
throw new ArgumentNullException("message");

            
if (Globals.IsNullorEmpty(message.Bcc))
                
return null;

            
return message.Bcc.Split(';');
        }


        
#endregion

    }

}


注: 按照常理,使用前先 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后,将它引用至项目中。

下载组件

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

using System;
using System.Web.Mail;

namespace YywMail
{
    
public class JMailSmtpMail : MySmtpMail
    
{
        
Fields#region Fields

        MyJmail.Message jmail 
= null;

        
#endregion


        
Methods#region Methods

        
public override void Open()
        
{
            jmail 
= new MyJmail.Message();
        }


        
public override bool Send(MailMessage message, string smtpServer, string serverUsername, string serverPassword, int smtpPort)
        
{
            
if (jmail == null)
                
throw new Exception("smtp is Closed!");
            
            
if (message == null)
                
throw new ArgumentNullException("message");

            DateTime t 
= DateTime.Now;

            
//Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false
            jmail.Silent = false;

            
//jmail创建的日志,前提loging属性设置为true
            jmail.Logging = true;

            
//字符集,缺省为"US-ASCII"
            jmail.Charset = base.DefaultCharset;

            
//信件的contentype. 缺省是"text/plain") : 字符串如果你以HTML格式发送邮件, 改为"text/html"即可。
            if (message.BodyFormat == MailFormat.Html)
                jmail.ContentType 
= "text/html";

            
            jmail.Priority 
= GetJmailPriority(message.Priority);

            
//添加收件人
            string[] toArray = MySmtpMail.GetTo(message);
            
if (toArray != null && toArray.Length > 0)
            
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics