最近工作原因需要维护 ASP.NET 1.1 的程序,本来用着 C# 2.0 System.Net.Mail namespace 发送邮件用得好好得,但 ASP.NET 1.1 里面的 System.Web.Mail 死活问题就是多,于是选择其它策略,封装了邮件发送的接口,再以其它方式来实现邮件发送,于是就有了下面这些文字。 定义抽象接口以封装所有实现: using System; namespace YywMail private string _defaultCharset = “GB2312”; #endregion protected string DefaultCharset protected int DefaultSmtpPort #endregion Methods#region Methods /**//// <summary> /**//// <summary> /**//// <summary> /**//// <summary> public abstract bool Send(MailMessage message, string smtpServer, string serverUsername, string serverPassword, int smtpPort); public static string[] GetTo(MailMessage message) if (Globals.IsNullorEmpty(message.To)) return message.To.Split(;); public static string[] GetCc(MailMessage message) if (Globals.IsNullorEmpty(message.Cc)) return message.Cc.Split(;); public static string[] GetBcc(MailMessage message) if (Globals.IsNullorEmpty(message.Bcc)) return message.Bcc.Split(;); #endregion 注: 按照常理,使用前先 Open() 一下,使用后也别忘了 Close() 实现方案一(Jmail 组件): 在 .NET 中使用 Jmail 需要如下设置: 下载组件 接下来就是实现类的编写了: using System; namespace YywMail MyJmail.Message jmail = null; #endregion Methods#region Methods public override void Open() public override bool Send(MailMessage message, string smtpServer, string serverUsername, string serverPassword, int smtpPort) DateTime t = DateTime.Now; //Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false //jmail创建的日志,前提loging属性设置为true //字符集,缺省为”US-ASCII” //信件的contentype. 缺省是”text/plain”) : 字符串如果你以HTML格式发送邮件, 改为”text/html”即可。 //添加收件人 for (int i = 0; i < toArray.Length; i++) if (!isAddedRecipient) string[] ccArray = MySmtpMail.GetCc(message); string[] bccArray = MySmtpMail.GetBcc(message); jmail.From = message.From; //发件人邮件用户名 if (message.BodyFormat == MailFormat.Html) jmail.Body += DateTime.Now.ToString(); smtpServer = String.Format(“{0}:{1}”, smtpServer, smtpPort); //jmail发送的方法 return result; public override void Close() if (priority == System.Web.Mail.MailPriority.High) if (priority == System.Web.Mail.MailPriority.Low) return 3; #endregion 实现方案二(OpenSmtp.Net 组件): 对于还没有接触过 OpenSmtp.Net 的朋友可以先 Google 下,这里不再做概念介绍,同时它还有一个孪生兄弟 OpenPop.Net,至于 OpenSmtp.Net 我们可以在 http://sourceforge.net/projects/opensmtp-net/ 下载到,目前最新版本是 1.11。 接下来开始进入主题: using System; namespace YywMail OpenSmtp.Mail.MailMessage msg = null; #endregion Methods#region Methods public override void Open() public override bool Send(System.Web.Mail.MailMessage message, string smtpServer, string serverUsername, string serverPassword, int smtpPort) if (message == null) SmtpConfig.VerifyAddresses = false; msg.From = from; //添加收件人 string[] ccArray = MySmtpMail.GetCc(message); string[] bccArray = MySmtpMail.GetBcc(message); msg.Subject = message.Subject; if (message.BodyFormat == System.Web.Mail.MailFormat.Html) string str1 = msg.HtmlBody; smtp.SendMail(msg); return true; private static string GetOpenSmtpPriority( System.Web.Mail.MailPriority priority) if (priority == System.Web.Mail.MailPriority.High) if (priority == System.Web.Mail.MailPriority.Low) return OpenSmtp.Mail.MailPriority.Normal; #endregion (上面两种实现方案已经足够满足目前的需求,有必要的话再继续扩展)
using System.Web.Mail;
{
public abstract class MySmtpMail
{
Fields#region Fields
private int _defaultSmtpPort = 25;
Properties#region Properties
{
get { return this._defaultCharset; }
}
{
get { return this._defaultSmtpPort;}
}
/// 获取默认实例
/// </summary>
/// <returns></returns>
public static MySmtpMail GetDefaultInstance()
{
// 此处可通过外部配置文件定义具体实现类型,再
// 通过 Activator.CreateInstance() 获取类型实例
}
/// 做一些初始化的工作
/// </summary>
public abstract void Open();
/// 销毁对象
/// </summary>
public abstract void Close();
/// 发送邮件
/// </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);
}
{
if (message == null)
throw new ArgumentNullException(“message”);
return null;
}
{
if (message == null)
throw new ArgumentNullException(“message”);
return null;
}
{
if (message == null)
throw new ArgumentNullException(“message”);
return null;
}
}
}
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.Web.Mail;
{
public class JMailSmtpMail : MySmtpMail
{
Fields#region Fields
{
jmail = new MyJmail.Message();
}
{
if (jmail == null)
throw new Exception(“smtp is Closed!”);
if (message == null)
throw new ArgumentNullException(“message”);
jmail.Silent = false;
jmail.Logging = true;
jmail.Charset = base.DefaultCharset;
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;
{
if (Globals.IsNullorEmpty(toArray[i]))
continue;
{
jmail.AddRecipient(toArray[i], String.Empty, String.Empty);
isAddedRecipient = true;
}
else
{
jmail.AddRecipientCC(toArray[i], String.Empty, String.Empty);
}
}
}
if (ccArray != null)
{
foreach (string cc in ccArray)
{
if (!Globals.IsNullorEmpty(cc))
jmail.AddRecipientCC(cc, String.Empty, String.Empty);
}
}
if (ccArray != null)
{
foreach (string bcc in bccArray)
{
if (!Globals.IsNullorEmpty(bcc))
jmail.AddRecipientBCC(bcc, String.Empty);
}
}
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;
jmail.Body += “<br><br>”;
else
jmail.Body += “\r\n\r\n”;
bool result = jmail.Send(smtpServer, false);
}
{
jmail.Close();
}
private static byte GetJmailPriority( System.Web.Mail.MailPriority priority)
{
// 邮件等级,1为加急,3为普通,5为低级
return 1;
return 5;
}
}
}
using System.Web.Mail;
using OpenSmtp.Mail;
{
public class OpenSmtpMail : MySmtpMail
{
Files#region Files
Smtp smtp = null;
{
msg = new OpenSmtp.Mail.MailMessage();
smtp = new Smtp();
}
{
if (msg == null || smtp == null)
throw new Exception(“smtp is Closed!”);
throw new ArgumentNullException(“message”);
EmailAddress from = new EmailAddress(message.From, message.From);
msg.Charset = base.DefaultCharset;
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);
}
}
if (ccArray != null)
{
foreach (string cc in ccArray)
{
if (!Globals.IsNullorEmpty(cc))
msg.AddRecipient(cc, AddressType.Cc);
}
}
if (ccArray != null)
{
foreach (string bcc in bccArray)
{
if (!Globals.IsNullorEmpty(bcc))
msg.AddRecipient(bcc, AddressType.Bcc);
}
}
{
msg.HtmlBody = message.Body + “<br><br>” + DateTime.Now.ToString();
}
else
{
msg.Body = message.Body + “\r\n\r\n” + DateTime.Now.ToString();;
}
string str2 = msg.Body;
smtp.Host = smtpServer;
smtp.Username = serverUsername;
smtp.Password = serverPassword;
smtp.Port = smtpPort;
}
public override void Close()
{
msg = null;
smtp = null;
}
{
// 邮件等级,1为加急,3为普通,5为低级
return OpenSmtp.Mail.MailPriority.High;
return OpenSmtp.Mail.MailPriority.Low;
}
}
}
实现方案三:
.net 1.1 下不使用 system.web.mail.smtpmail 发送邮件的其它选择_asp.net技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » .net 1.1 下不使用 system.web.mail.smtpmail 发送邮件的其它选择_asp.net技巧
相关推荐
-      对.net framework 反射的反思_asp.net技巧
-      .net3.5和vs2008中的asp.net ajax_asp.net技巧
-      使用asp.net ajax框架扩展html map控件_asp.net技巧
-      asp.net应用程序资源访问安全模型_asp.net技巧
-      photoshop初学者轻松绘制螺旋漩涡特效_photoshop教程
-      photoshop通道结合图层模式抠狗尾巴草_photoshop教程
-      web.config详解+asp.net优化_asp.net技巧
-      asp.net中多彩下拉框的实现_asp.net技巧