ASP.NET发邮件方法-飞外

第一种方法:使用CDOSYS

引入名称空间(NameSpace)System.Web.Mail类库
(1)它有三个类:SmtpMail、MailMessage和MailAttachment。这三个对象本文的示例程序代码中都应用到了!
1. MailMessage ,提供属性和方法来创建一个邮件消息对象。(Provides properties and methods for constructing an e-mail message.)
2. MailAttachments – 提供属性和方法来创建一个邮件附件对象。(Provides properties and methods for constructing an e-mail attachment.)
3. SmtpMail – 提供属性和方法通过使用windows 2000 CDOSYS 的消息组件的联合数据对象来发送邮件消息)。(Provides properties and methods for sending messages using the Collaboration Data Objects for Windows 2000 (CDOSYS) message component)
(2)各个类的属性。
1.先简单介绍SmtpMail的属性: SmtpServer -- SMTP的地址。
2.主要来介绍MailMessage对象的属性
From -- 发送邮件的地址
To -- 接受邮件的地址
Subject -- 邮件的标题
Priority -- 邮件的优先级(有效值为High,Low,Normal)
Attachments -- 返回一个集合,代表附件
Bcc -- 密送地址
Cc -- 抄送地址
Body -- 获取或是设置电子邮件消息的内容
BodyFormat -- 获取或是设置MailFormat的枚举值,此值指定消息体邮件的格式(Html格式、Text格式)
Bodyencoding -- 指定消息的编码方式编码(主要有Base64,UUencode)
其他几个不重要的省略。 随便提到密送和抄送的区别:密送就是你群发邮件时收邮件的人无法看到你发给了多少人以及他们的邮件地址,抄送就是群发邮件时收邮件的人则可以看到你发给了多少人以及他们的邮件地址。
(3)SmtpMail类的Send方法,它的目的就是发送邮件,有两个重载方法。
1. SmtpMail.Send("发送邮件的地址","接受邮件的地址","邮件的标题","邮件消息的内容") 这个方法很简单,不适合发送带附件的邮件。
2. SmtpMail.Send(MailMessage) 此方法复杂、灵活,适合发送附件,而且可以设置MailMessage对象的各种属性值。 如果我们用ASP.NET写一个邮件发送的程序,那么首先应该如何得到SMTP。有两种方法:第一种方法调用目前知名的邮件服务提供商的SMTP,比如飞外、飞外、网易的免费电子邮箱的SMTP;第二种方法是自己装一个SMTP虚拟服务器,这个在安装IIS时一起装上去的(安装过程就省略了:-) )。
一、在ASP.NET利用知名的邮件服务提供商的SMTP来发送邮件
首先需要去他们的邮件站点上注册免费邮箱,因为你要使用邮件服务提供商的SMTP,他们需要对身份进行验证,这样可以避免产生大量的垃圾邮件。假设我们在飞外的邮件站点(mail.feedwhy.com.cn)上注册了一个免费电子邮件,用户名是mysina,密码是chenjie.该帐号为虚构的,请使用自己注册的用户名称和密码代替。我们在飞外的邮件站点获知它的SMTP地址是:smtp.feedwhy.com.cn。我们需要向scucj@126.com(我的邮箱地址)发送邮件。 那么利用ASP.NET(C#)发送邮件的核心代码如下:
//核心代码开始
using System.Web.Mail;
MailMessage objMailMessage;
MailAttachment objMailAttachment;
// 创建一个附件对象
objMailAttachment = new MailAttachment( "d:\\test.txt" );//发送邮件的附件
// 创建邮件消息
objMailMessage = new MailMessage();
objMailMessage.From = "mysina@feedwhy.com";//源邮件地址
objMailMessage.To = "scucj@126.com";//目的邮件地址,也就是发给我哈
objMailMessage.Subject = "邮件发送标题:你好";//发送邮件的标题
objMailMessage.Body = "邮件发送标内容:测试一下是否发送成功!";//发送邮件的内容
objMailMessage.Attachments.Add( objMailAttachment );//将附件附加到邮件消息对象中
//接着利用sina的SMTP来发送邮件,需要使用Microsoft .NET Framework SDK v1.1和它以上的版本
//基本权限
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//用户名
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "mysina")
//密码
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "chenjie");
/如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为: 554 : Client host rejected: Access denied
//SMTP地址
SmtpMail.SmtpServer = "smtp.feedwhy.com.cn";
//开始发送邮件
SmtpMail.Send( objMailMessage );
//核心代码结束
二、在ASP.NET利用本机的SMTP虚拟服务器的SMTP来发送邮件
首先说一下SMTP配置。
(1)右键点击“SMTP虚拟服务器”选择“属性”- 在“常规”选项卡中设置“IP地址(P)”,我设置的是192.168.1.100。
(2)选择“访问”选项卡,点击“中继”,选上“仅以下列表”(默认是被选上的),点击“添加”,在“单台计算机”中加入192.168.1.100。
提示,如果没有完成(2),则会出现大家常见的一种错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为: 550 5.7.1 Unable to relay for scucj@126.com (友情提示一下:错误中的邮件地址有所不同) 然后开始核心代码,其实和方法(一)的差不多。与(一)的主要区别在于:1.SMTP的不同,2.objMailMessage.From中本方法可以随便填写,但是(一)中别随便填写那么利用ASP.NET(C#)发送邮件的核心代码如下:
/核心代码开始
using System.Web.Mail;
MailMessage objMailMessage;
MailAttachment objMailAttachment;
// 创建一个附件对象
objMailAttachment = new MailAttachment( "d:\\test.txt" );//发送邮件的附件
// 创建邮件消息
objMailMessage = new MailMessage();
objMailMessage.From = "mysina@feedwhy.com";//源邮件地址
objMailMessage.To = "scucj@126.com";//目的邮件地址,也就是发给我哈
objMailMessage.Subject = "邮件发送标题:你好";//发送邮件的标题
objMailMessage.Body = "邮件发送标内容:测试一下是否发送成功!";//发送邮件的内容
objMailMessage.Attachments.Add( objMailAttachment );//将附件附加到邮件消息对象中
//SMTP地址
SmtpMail.SmtpServer = "192.168.1.100";
//开始发送邮件
SmtpMail.Send( objMailMessage );

第二种方法:使用LOTUS组件

必备条件:

1、本地有一个Lotus Notes的客户端,得到信息如下:

a:服务器的描述,例如:srvc7/srvc/cag

b:账号文件信息,例如:mail102\zhangsan1.nsf

c:密码, 例如:MyPassword

另外假设邮箱是:zhangsan1@mydomian.com

2、在启动VS时,添加引用,添加Com引用。由于你已经安装了Lotusnotes客户端,你可以在COM引用中看到Lotus Notes的引用,添加它,你的引用中会一个Domino的dll

3、在你的项目的命名空间中加上using Domino;

代码如下:

参数意义:

PSendTo:接受邮件地址

PCopyTo:抄送地址

PBlindCopyTo:暗送地址

PSubject:标题

PRemark:(这个参数我也不是太清楚)

PBody:正文

PAtt:附件

public static Boolean SendNotesMail(string PsendTo, string PcopyTo, string PBlindCopyTo, string PSubject, String PRemark, string PBody, string PAtt)
{
bool result;
try
{
NotesSession session = null;
session = new NotesSessionClass();
if (session == null)
{
return false;
}

try
{
NotesDatabase oNotesDatabase;
NotesDocument oNotesDocument;
string strNotesPassword = "MyPassword"; //密码(password used by COM to pass to the Notes client login)
string strNotesService = @"srvc7/srvc/cag"; //服务器的描述
string strNotesDataBase = @"mail102\zhangsan1.nsf"; //账号文件信息
session.Initialize(strNotesPassword); //Initialise session by passing a password. Lotus Notes will not load
oNotesDatabase = session.GetDatabase(strNotesService, strNotesDataBase, false); //create a database handle to the database you wish to send the mail message from.
if (!oNotesDatabase.IsOpen)
oNotesDatabase.Open();
oNotesDocument = oNotesDatabase.CreateDocument();
string[] arrPerson1 = PsendTo.Split(',');
oNotesDocument.AppendItemValue("SendTo", arrPerson1);
if (PcopyTo.Trim()!= "")
{
string[] arrPerson2 = PcopyTo.Split(',');
oNotesDocument.AppendItemValue("CopyTo", arrPerson2);
}
if (PBlindCopyTo.Trim()!= "")
{
string[] arrPerson3 = PBlindCopyTo.Split(',');
oNotesDocument.AppendItemValue("BlindCopyTo", arrPerson3);
}
oNotesDocument.AppendItemValue("Subject", PSubject);
oNotesDocument.ReplaceItemValue("Remark", PRemark);
oNotesDocument.AppendItemValue("From", "memo");
string psenddate = System.DateTime.Now.ToString("yyyy/MM/dd");
oNotesDocument.ReplaceItemValue("SendDate", psenddate);
session.ConvertMime = false;
Domino.NotesMIMEEntity bodyHtml = oNotesDocument.CreateMIMEEntity("Body");
Domino.NotesStream notesStream = session.CreateStream();
notesStream.WriteText(PBody, Domino.EOL_TYPE.EOL_NONE);
bodyHtml.SetContentFromText(notesStream, "text/html, charset=UIF-8", Domino.MIME_ENCODING.ENC_IDENTITY_BINARY);
notesStream.Truncate();
notesStream.Close();
oNotesDocument.CloseMIMEEntities(true, "Body");
session.ConvertMime = true;
oNotesDocument.ComputeWithForm(false, false);
NotesRichTextItem arrachment = oNotesDocument.CreateRichTextItem("attachment");
arrachment.EmbedObject(EMBED_TYPE.EMBED_ATTACHMENT,"", PAtt,"attachment");
object MailTo = arrPerson1;
oNotesDocument.Send(false, ref MailTo);
result = true;

}
catch(Exception ex)
{
return false;
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(session);
}
}
catch(Exception ex)
{
result = false;
}

return result;
}