您的位置:首页 > 编程语言 > ASP

在Asp.Net中发送邮件的常用方法:

2006-01-06 16:14 519 查看
1.SMTP 方式:
public void SendMailLog(string sourceName, string message)
{
MailMessage newMail = new MailMessage();
newMail.Subject = sourceName;
newMail.From="**@sina.com";
newMail.To = "**@sina.com";
newMail.Body = message;
newMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
newMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "******"); //set your username here
newMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "*****"); //set your password here
newMail.Priority=MailPriority.High;
SmtpMail.SmtpServer = "smtp.sina.com.cn";
SmtpMail.Send(newMail);
}
附:在SMTP下使用线程发送邮件:
http://www.dotnetspider.com/kb/Article1565.aspx
2.使用Jmail组件
public string SendPassword()
{
jmail.Message MailObj=new jmail.MessageClass();
MailObj.From="xxxx@163.com"; //发件人的地址
MailObj.Logging=true;
MailObj.MailServerUserName="xxxx"; //发件人用户名
MailObj.MailServerPassWord="xxxxx"; //服务器验证
MailObj.HTMLBody=" Test";
MailObj.Charset="gb2312";
MailObj.Subject="HELLO";
MailObj.FromName="Hey";
MailObj.AddRecipient(xxxx@xxxx.com,"User","A"); //添加接收人
MailObj.Priority=3;

//发送
if(MailObj.Send("smtp.163.com",false))
{
return "Success";
}
else
{
return "Fail";

}
3.ASP.NET结合COM组件发送Email
在系统目录(如c:/winnt或c:/windows)的system32子目录中可以找到一个名称为cdosys.dll的文件,我们可以通过ASP.NET调用此COM组件来实现Email的发送。cdosys构建在SMTP协议和NNTP协议之上,并且作为Windows2000 Server的组件被安装,当然我们也可以使用Exchange2000中cdoex.dll来实现发送邮件的机制,由于cdosys.dll内嵌到了操作系统中,所以不用再去注册相应的其他邮件发送程序比如jmail等。
  1、新建一个项目文件
  2、添加引用系统目录下的cdosys.dll文件,在引用中会发现添加了两个要用到的接口:CDO,ADODB
  3、添加新项文件SendMail.aspx,在其页面上放置三个Label,三个Textbox,作用分别为收件人地址、主题、内容,放置一个Button按钮。
  4、切换到代码页,创建一下内容

使用CDO发送(Microsoft CDO)
namespace GoldWeb.WebUI
{
using System;
using System.Text;
using CDO;
using GoldWeb.Model;
using GoldWeb.SystemFramework;
/// <summary>
/// CDO Email操作类
/// </summary>
public class CdoMail
{
/// <summary>
/// 构造函数
/// </summary>
public CdoMail()
{
}
public static int Send(AccountModel accountModel)
{
try
{
string email = ApplicationString.HtmlEncodeString(accountModel.Email);
string userName = ApplicationString.HtmlEncodeString(accountModel.UserName);
string password = ApplicationString.HtmlEncodeString(accountModel.Password);
Message oMsg = new MessageClass();
oMsg.From = ""; // 邮件来源
oMsg.To = email;
oMsg.Subject = "确认函";
StringBuilder sb = new StringBuilder();
sb.Append("你的邮件内容");
oMsg.HTMLBody = sb.ToString();
IConfiguration iConfg = oMsg.Configuration;
ADODB.Fields oFields = iConfg.Fields;
oFields[".Value]http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
oFields[".Value]http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = email; //sender mail
oFields[".Value]http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = "xx@xx.com"; //email account
oFields[".Value]http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = ""; // 邮箱用户名
oFields[".Value]http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = ""; // 邮箱密码
oFields[".Value]http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1;
//value=0 代表Anonymous验证方式(不需要验证)
//value=1 代表Basic验证方式(使用basic (clear-text) authentication.
//The configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.)
//Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express)
oFields[".Value]http://schemas.microsoft.com/cdo/configuration/languagecode"].Value = 0x0804;
oFields[".Value]http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = ""; // 邮件服务器地址
oFields.Update();
oMsg.BodyPart.Charset="gb2312";
oMsg.HTMLBodyPart.Charset="gb2312";
oMsg.Send();
oMsg = null;

return 0;
}
catch
{
return 1;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: