asp.net中实现在线发送电子邮件的方法
 
[ 双击自动滚屏 ] [ 打印本页 ]  [ 发布日期: 2006-04-27]  [ 被阅次数    672  次]    [字体: ]  
1.使用SMTP
MailMessage Mailer = new MailMessage();
Mailer.From = "服务器域名";
Mailer.To = "发送的邮箱";
Mailer.Subject = "邮件名";
Mailer.Body = "邮件内容";
Mailer.BodyFormat = System.Web.Mail.MailFormat.Text;
SmtpMail.SmtpServer ="邮件服务器地址";
SmtpMail.Send(Mailer);
Label1.Text = "密码已经成功发送到你的邮箱中!";   //页面提示

2.使用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["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
    oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = email;   //sender mail
    oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = "xx@xx.com"; //email account
    oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = ""; // 邮箱用户名
    oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = ""; // 邮箱密码 
    oFields["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["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value = 0x0804;
    oFields["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;
   }
  }
 
3.使用Jmail组件
public string SendPassword()
  {
   jmail.Message MailObj=new jmail.MessageClass();
   MailObj.From="xxxx@163com";  //发件人的地址
   MailObj.Logging=true;
   MailObj.MailServerUserName="xxxx";   //发件人用户名
   MailObj.MailServerPassWord="xxxxxxxx"; //服务器验证
   MailObj.HTMLBody=" Hello,欢迎查看此贴,^_^:";
   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";
   
   }