.net盛行一时的时候,在asp.net里发送邮件也成了个不大也不小的问题。虽然.net里自己带了发邮件的组件,但使用它的人却不知道有多少。可能我对这些了解不是很深入,但在网络上搜索的时候,发现有很多人想到了把JMail的COM组件封装在.net的组件下,用它来发送邮件。前些时候我也这样做过了,效果还不错。这样一来,不管服务器是不是安装了JMail组件,只要服务器支持asp.net就可以用它来发送邮件了。
昨天在JMail的官方网站上看到了最新的JMail的.net版本,这个组件完全是用.net开发的,与.net可以是完全无缝的结合吧。于是决定在自己的项目里使用它了。然而它在每个邮件后添加了一个版本信息,让我很郁闷。至于是不是删除这个信息到不是本质问题,问题是我想两个组件都使用,或者在不同的条件下合理的使用它们,于是我改用接口来完成这项工作了。
using System;
![](http://blog.aspcool.com/Images/OutliningIndicators/None.gif)
namespace Webb.WAVE.Controls
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://blog.aspcool.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Summary description for Webb.
/// </summary>
public interface IEMail:IDisposable
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string ServerName
{get;set;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string ServerLoginName
{get;set;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string ServerLoginPassword
{set;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string Subject
{set;get;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string Body
{set;get;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string HTMLBody
{set;get;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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;
![](http://blog.aspcool.com/Images/OutliningIndicators/None.gif)
namespace Webb.WAVE.Controls
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://blog.aspcool.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Summary description for Webb.
/// </summary>
public class JMailCOM :IEMail
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ContractedSubBlock.gif)
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()
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_message = new Webb.JMail.Message();
}
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
public JMailCOM(bool i_systemServer)
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_message = new Webb.JMail.Message();
DataTable m_table = new DataTable();
WaveSystem.LoadSystemData(m_table);
foreach(DataRow m_row in m_table.Rows)
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(m_row[1].ToString()=="SystemEmailServerPassword")
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_password = m_row[2].ToString();
}
else if(m_row[1].ToString()=="SystemEmailServerLoginName")
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_loginName = m_row[2].ToString();
}
else if(m_row[1].ToString()=="SystemEmailServer")
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_server = m_row[2].ToString();
}
}
m_table.Dispose();
}
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
![](http://blog.aspcool.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IEMail Members#region IEMail Members
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
public string ServerName
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this.m_server;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.m_server = value;}
}
public string ServerLoginName
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this.m_loginName;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.m_loginName = value;}
}
public string ServerLoginPassword
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.m_password = value;}
}
public string Subject
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this.m_message.Subject;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.m_message.Subject = value;}
}
public string Body
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this.m_message.Body;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.m_message.Body = value;}
}
public string HTMLBody
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this.m_message.HTMLBody;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.m_message.HTMLBody = value;}
}
public string From
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this.m_from;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.m_from = value;}
}
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
public void AddToAddress(string i_address)
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_message.AddRecipient(i_address,null,null);
}
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
public void AddCcAddress(string i_address)
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_message.AddRecipientCC(i_address,null,null);
}
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
public void SendEMail()
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_message.MailServerPassWord = this.m_password;
this.m_message.MailServerUserName = this.m_loginName;
this.m_message.From = this.m_loginName;
try
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_message.Send(this.m_server,false);
}
catch(Exception ex)
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WaveTrace.TraceMsg(ex.Message);
throw ex;
}
}
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
![](http://blog.aspcool.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IDisposable Members#region IDisposable Members
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
public void Dispose()
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// TODO: Add JMailCOM.Dispose implementation
this.m_message.Clear();
this.m_message = null;
}
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
}
![](http://blog.aspcool.com/Images/OutliningIndicators/None.gif)
.net类
using System;
using Dimac.JMail;
using System.Data;
![](http://blog.aspcool.com/Images/OutliningIndicators/None.gif)
namespace Webb.WAVE.Controls
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://blog.aspcool.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
///
/// </summary>
public class JMailDotNet:IEMail
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Fields#region Fields
private string m_server;
private string m_loginName;
private string m_password;
private string m_from;
private Message m_message;
#endregion
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
![](http://blog.aspcool.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Properties#region Properties
// public Message MailMessage
// {
// get{return this.m_message;}
// set{this.m_message = value;}
// }
#endregion
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
public JMailDotNet()
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_message = new Message();
}
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
public JMailDotNet(bool i_systemServer)
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_message = new Message();
DataTable m_table = new DataTable();
WaveSystem.LoadSystemData(m_table);
foreach(DataRow m_row in m_table.Rows)
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(m_row[1].ToString()=="SystemEmailServerPassword")
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_password = m_row[2].ToString();
}
else if(m_row[1].ToString()=="SystemEmailServerLoginName")
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_loginName = m_row[2].ToString();
}
else if(m_row[1].ToString()=="SystemEmailServer")
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_server = m_row[2].ToString();
}
}
m_table.Dispose();
}
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
![](http://blog.aspcool.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IEMail Members#region IEMail Members
public string ServerName
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this.m_server;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.m_server = value;}
}
public string ServerLoginName
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this.m_loginName;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.m_loginName = value;}
}
public string ServerLoginPassword
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.m_password = value;}
}
public string Subject
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this.m_message.Subject;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.m_message.Subject = value;}
}
public string Body
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this.m_message.BodyText;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.m_message.BodyText = value;}
}
public string HTMLBody
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this.m_message.BodyHtml;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.m_message.BodyHtml = value;}
}
public string From
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this.m_from;}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.m_from = value;}
}
public void AddToAddress(string i_address)
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_message.To.Add(new Address(i_address));
}
public void AddCcAddress(string i_address)
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_message.Cc.Add(new Address(i_address));
}
public void SendEMail()
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_SMTPServer.Send(this.m_message);
}
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
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)
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WaveTrace.TraceMsg(ex.Message);
throw new Exception("EMail sent error, all message send faild.");
}
finally
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_SMTPServer = null;
}
}
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
![](http://blog.aspcool.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IDisposable Members#region IDisposable Members
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
public void Dispose()
![](http://blog.aspcool.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_message = null;
}
![](http://blog.aspcool.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
}
![](http://blog.aspcool.com/Images/OutliningIndicators/None.gif)
好了,想用哪个发送邮件就用哪了。
文章来源:
http://computer.mblogger.cn/wucountry/posts/49425.aspx