最近工作原因需要維護 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;
r> #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)
{
bool isAddedRecipIEnt = false;
for (int i = 0; i < toArray.Length; i++)
{
if (Globals.IsNullorEmpty(toArray[i]))
continue;
if (!isAddedRecipIEnt)
{
jmail.AddRecipIEnt(toArray[i], String.Empty, String.Empty);
isAddedRecipIEnt = true;
}
else
{
jmail.AddRecipIEntCC(toArray[i], String.Empty, String.Empty);
}
}
}
string[] ccArray = MySmtpMail.GetCc(message);
if (ccArray != null)
{
foreach (string cc in ccArray)
{
if (!Globals.IsNullorEmpty(cc))
jmail.AddRecipIEntCC(cc, String.Empty, String.Empty);
}
}
string[] bccArray = MySmtpMail.GetBcc(message);
if (ccArray != null)
{
foreach (string bcc in bccArray)
{
if (!Globals.IsNullorEmpty(bcc))
jmail.AddRecipIEntBCC(bcc, String.Empty);
}
}
jmail.From = message.From;
//發件人郵件用戶名
jmail.MailServerUserName = serverUsername;
//發件人郵件密碼
jmail.MailServerPassWord = serverPassWord;
//設置郵件標題
jmail.Subject = message.Subject;
//郵件添加附件,(多附件的話,可以再加一條jmail.AddAttachment( "c:\\test.jpg",true,null);)就可以搞定了。[注]:加了附件,講把上面的jmail.ContentType="text/Html";刪掉。否則會在郵件裡出現亂碼。
//jmail.AddAttachment("c:\\test.jpg", true, null);
//郵件內容
jmail.Body = message.Body;
if (message.BodyFormat == MailFormat.Html)
jmail.Body += "<br><br>";
else
jmail.Body += "\r\n\r\n";
jmail.Body += DateTime.Now.ToString();
smtpServer = String.Format("{0}:{1}", smtpServer, smtpPort);
//jmail發送的方法
bool result = jmail.Send(smtpServer, false);
return result;
}
public override void Close()
{
jmail.Close();
}
private static byte GetJmailPriority( System.Web.Mail.MailPriority priority)
{
// 郵件等級,1為加急,3為普通,5為低級
if (priority == System.Web.Mail.MailPriority.High)
return 1; >
if (priority == System.Web.Mail.MailPriority.Low)
return 5;
return 3;
}
#endregion
}
}
實現方案二(OpenSmtp.Net 組件):
對於還沒有接觸過 OpenSmtp.Net 的朋友可以先 Google 下,這裡不再做概念介紹,同時它還有一個孿生兄弟 OpenPop.Net,至於 OpenSmtp.Net 我們可以在 http://sourceforge.Net/projects/opensmtp-net/ 下載到,目前最新版本是 1.11。
接下來開始進入主題:
using System;
using System.Web.Mail;
using OpenSmtp.Mail;
namespace YywMail
{
public class OpenSmtpMail : MySmtpMail
{
Files#region Files
OpenSmtp.Mail.MailMessage msg = null;
Smtp smtp = null;
#endregion
Methods#region Methods
public override void Open()
{
msg = new OpenSmtp.Mail.MailMessage();
smtp = new Smtp();
}
public override bool Send(System.Web.Mail.MailMessage message, string smtpServer, string serverUsername, string serverPassWord, int smtpPort)
{
if (msg == null || smtp == null)
throw new Exception("smtp is Closed!");
if (message == null)
throw new ArgumentNullException("message");
SmtpConfig.VerifyAddresses = false;
EmailAddress from = new EmailAddress(message.From, message.From);
msg.Charset = base.DefaultCharset;
msg.From = from;
msg.Priority = GetOpenSmtpPriority(message.Priority);
//添加收件人
string[] toArray = MySmtpMail.GetTo(message);
if (toArray != null)
{
foreach (string to in toArray)
{
if (!Globals.IsNullorEmpty(to))
msg.AddRecipIEnt(to, AddressType.To);
}
}
string[] ccArray = MySmtpMail.GetCc(message);
if (ccArray != null)
{
foreach (string cc in ccArray)
{
if (!Globals.IsNullorEmpty(cc))
msg.AddRecipIEnt(cc, AddressType.Cc);
}
}
string[] bccArray = MySmtpMail.GetBcc(message);
if (ccArray != null)
{
foreach (string bcc in bccArray)
{
if (!Globals.IsNullorEmpty(bcc))
msg.AddRecipIEnt(bcc, AddressType.Bcc);
}
}
msg.Subject = message.Subject;
if (message.BodyFormat == System.Web.Mail.MailFormat.Html)
{
msg.HtmlBody = message.Body + "<br><br>" + DateTime.Now.ToString();
}
else
{
msg.Body = message.Body + "\r\n\r\n" + DateTime.Now.ToString();;
}
string str1 = msg.HtmlBody;
string str2 = msg.Body;
smtp.Host = smtpServer;
smtp.Username = serverUsername;
smtp.Password = serverPassWord;
smtp.Port = smtpPort;
<br> smtp.SendMail(msg);
return true;
}
public override void Close()
{
msg = null;
smtp = null;
}
private static string GetOpenSmtpPriority( System.Web.Mail.MailPriority priority)
{
// 郵件等級,1為加急,3為普通,5為低級
if (priority == System.Web.Mail.MailPriority.High)
return OpenSmtp.Mail.MailPriority.High;
if (priority == System.Web.Mail.MailPriority.Low)
return OpenSmtp.Mail.MailPriority.Low;
return OpenSmtp.Mail.MailPriority.Normal;
}
#endregion
}
}
實現方案三:
(上面兩種實現方案已經足夠滿足目前的需求,有必要的話再繼續擴展)