.net盛行一时的时候,在asp.net里发送邮件也成了个不大也不小的问题。虽然.net里自己带了发邮件的组件,但使用它的人却不知道有多少。可能我对这些了解不是很深入,但在网络上搜索的时候,发现有很多人想到了把JMail的COM组件封装在.net的组件下,用它来发送邮件。前些时候我也这样做过了,效果还不错。这样一来,不管服务器是不是安装了JMail组件,只要服务器支持asp.net就可以用它来发送邮件了。
昨天在JMail的官方网站上看到了最新的JMail的.net版本,这个组件完全是用.net开发的,与.net可以是完全无缝的结合吧。于是决定在自己的项目里使用它了。然而它在每个邮件后添加了一个版本信息,让我很郁闷。至于是不是删除这个信息到不是本质问题,问题是我想两个组件都使用,或者在不同的条件下合理的使用它们,于是我改用接口来完成这项工作了。
using System;

namespace Webb.WAVE.Controls


{

/**//// <summary>
/// Summary description for Webb.
/// </summary>
public interface IEMail:IDisposable

{

string ServerName
{get;set;}

string ServerLoginName
{get;set;}

string ServerLoginPassword
{set;}

string Subject
{set;get;}

string Body
{set;get;}

string HTMLBody
{set;get;}

string From
{set;get;}
//
void AddToAddress(string i_address);
void AddCcAddress(string i_address);
void SendEMail();
}
}
实现它的类可以写两个,这样一来就可以自己选择性的使用它们了:COM类
using System;
using Dimac.JMail;
using System.Data;
using Webb.JMail;

namespace Webb.WAVE.Controls


{

/**//// <summary>
/// Summary description for Webb.
/// </summary>
public class JMailCOM :IEMail

{

Fields#region Fields
private string m_server;
private string m_loginName;
private string m_password;
private string m_from;
private Webb.JMail.Message m_message;
#endregion
public JMailCOM()

{
m_message = new Webb.JMail.Message();
}

public JMailCOM(bool i_systemServer)

{
m_message = new Webb.JMail.Message();
DataTable m_table = new DataTable();
WaveSystem.LoadSystemData(m_table);
foreach(DataRow m_row in m_table.Rows)

{
if(m_row[1].ToString()=="SystemEmailServerPassword")

{
this.m_password = m_row[2].ToString();
}
else if(m_row[1].ToString()=="SystemEmailServerLoginName")

{
this.m_loginName = m_row[2].ToString();
}
else if(m_row[1].ToString()=="SystemEmailServer")

{
this.m_server = m_row[2].ToString();
}
}
m_table.Dispose();
}




IEMail Members#region IEMail Members

public string ServerName

{

get
{return this.m_server;}

set
{this.m_server = value;}
}
public string ServerLoginName

{

get
{return this.m_loginName;}

set
{this.m_loginName = value;}
}
public string ServerLoginPassword

{

set
{this.m_password = value;}
}
public string Subject

{

get
{return this.m_message.Subject;}

set
{this.m_message.Subject = value;}
}
public string Body

{

get
{return this.m_message.Body;}

set
{this.m_message.Body = value;}
}
public string HTMLBody

{

get
{return this.m_message.HTMLBody;}

set
{this.m_message.HTMLBody = value;}
}
public string From

{

get
{return this.m_from;}

set
{this.m_from = value;}
}

public void AddToAddress(string i_address)

{
this.m_message.AddRecipient(i_address,null,null);
}

public void AddCcAddress(string i_address)

{
this.m_message.AddRecipientCC(i_address,null,null);
}

public void SendEMail()

{
this.m_message.MailServerPassWord = this.m_password;
this.m_message.MailServerUserName = this.m_loginName;
this.m_message.From = this.m_loginName;
try

{
this.m_message.Send(this.m_server,false);
}
catch(Exception ex)

{
WaveTrace.TraceMsg(ex.Message);
throw ex;
}
}

#endregion


IDisposable Members#region IDisposable Members

public void Dispose()

{
// TODO: Add JMailCOM.Dispose implementation
this.m_message.Clear();
this.m_message = null;
}

#endregion
}
}

.net类
using System;
using Dimac.JMail;
using System.Data;

namespace Webb.WAVE.Controls


{

/**//// <summary>
///
/// </summary>
public class JMailDotNet:IEMail

{

Fields#region Fields
private string m_server;
private string m_loginName;
private string m_password;
private string m_from;
private Message m_message;
#endregion


Properties#region Properties
// public Message MailMessage
// {
// get{return this.m_message;}
// set{this.m_message = value;}
// }
#endregion

public JMailDotNet()

{
m_message = new Message();
}

public JMailDotNet(bool i_systemServer)

{
m_message = new Message();
DataTable m_table = new DataTable();
WaveSystem.LoadSystemData(m_table);
foreach(DataRow m_row in m_table.Rows)

{
if(m_row[1].ToString()=="SystemEmailServerPassword")

{
this.m_password = m_row[2].ToString();
}
else if(m_row[1].ToString()=="SystemEmailServerLoginName")

{
this.m_loginName = m_row[2].ToString();
}
else if(m_row[1].ToString()=="SystemEmailServer")

{
this.m_server = m_row[2].ToString();
}
}
m_table.Dispose();
}


IEMail Members#region IEMail Members
public string ServerName

{

get
{return this.m_server;}

set
{this.m_server = value;}
}
public string ServerLoginName

{

get
{return this.m_loginName;}

set
{this.m_loginName = value;}
}
public string ServerLoginPassword

{

set
{this.m_password = value;}
}
public string Subject

{

get
{return this.m_message.Subject;}

set
{this.m_message.Subject = value;}
}
public string Body

{

get
{return this.m_message.BodyText;}

set
{this.m_message.BodyText = value;}
}
public string HTMLBody

{

get
{return this.m_message.BodyHtml;}

set
{this.m_message.BodyHtml = value;}
}
public string From

{

get
{return this.m_from;}

set
{this.m_from = value;}
}
public void AddToAddress(string i_address)

{
this.m_message.To.Add(new Address(i_address));
}
public void AddCcAddress(string i_address)

{
this.m_message.Cc.Add(new Address(i_address));
}
public void SendEMail()

{
Smtp m_SMTPServer = new Smtp();
m_SMTPServer.Domain = this.m_server;
m_SMTPServer.Password = this.m_password;
m_SMTPServer.UserName = this.m_loginName;
m_SMTPServer.HostName = this.m_server;
this.m_message.From = new Address(this.m_loginName);
m_SMTPServer.Authentication =SmtpAuthentication.Login;
try

{
m_SMTPServer.Send(this.m_message);
}

/**//*
catch(SmtpAuthNotSupportedException ex)
{
WaveTrace.TraceMsg(ex.Message);
//throw new Exception("EMail sent error, all message send faild.");
}
catch(LoginFailedException ex)
{
WaveTrace.TraceMsg(ex.Message);
//throw new Exception("EMail sent error, all message send faild.");
}
catch(SmtpException ex)
{
WaveTrace.TraceMsg(ex.Message);
//throw new Exception("EMail sent error, all message send faild.");
}
catch(ConnectionException ex)
{
WaveTrace.TraceMsg(ex.Message);
//throw new Exception("EMail sent error, all message send faild.");
}
catch(ArgumentNullException ex)
{
WaveTrace.TraceMsg(ex.Message);
//throw new Exception("EMail sent error, all message send faild.");
}
*/
catch(JMailException ex)

{
WaveTrace.TraceMsg(ex.Message);
throw new Exception("EMail sent error, all message send faild.");
}
finally

{
m_SMTPServer = null;
}
}

#endregion


IDisposable Members#region IDisposable Members

public void Dispose()

{
this.m_message = null;
}

#endregion
}
}

好了,想用哪个发送邮件就用哪了。
文章来源:
http://computer.mblogger.cn/wucountry/posts/49425.aspx