.NET 1.1 下不使用 System.Web.Mail.SmtpMail 发…

2008-02-22 09:41:29来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


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;

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:.NET框架下Oracle到SQL Server迁移

下一篇:Asp.Net2.0中实现多任务异步页的一点提示