手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp.Net编程>列表

.NET 1.1 下不使用 System.Web.Mail.SmtpMail 发送邮件的其它选择

来源:互联网 作者:west263.com 时间:2008-02-22
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

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;

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!