现在的位置: 首页 > 综合 > 正文

发送邮件成功率最高代码(转载,个人感觉不错)

2012年09月29日 ⁄ 综合 ⁄ 共 2509字 ⁄ 字号 评论关闭
发送邮件成功率最高代码(转载,个人感觉不错)

自己写的一个使用cdo发送邮件的类
using System;
using System.Web.Mail;
using CDO;
namespace admin
{
 /// <summary>
 /// MailSender2 的摘要说明。
 /// </summary>
 public class MailSender
 {
  public string Server
  {
   get { return server; }
   set { if (value != server) server = value; }
  } private string server = "";
  /// <summary>
  /// 用户名 [如果需要身份验证的话]
  /// </summary>
  public string UserName
  {
   get { return userName; }
   set { if (value != userName) userName = value; }
  } private string userName = "";
  /// <summary>
  /// 密码 [如果需要身份验证的话]
  /// </summary>
  public string Password
  {
   get { return password; }
   set { if (value != password) password = value; }
  } private string password = "";
  /// <summary>
  /// 发件人地址
  /// </summary>
  public string From
  {
   get { return from; }
   set { if (value != from) from = value;}
  } private string from = "";
  /// <summary>
  /// 收件人地址
  /// </summary>
  public string To
  {
   get { return to; }
   set { if (value != to) to = value;}
  } private string to = "";
  /// <summary>
  /// 邮件的主题
  /// </summary>
  public string Subject
  {
   get { return subject; }
   set { if (value != subject) subject = value; }
  } private string subject = "";
  /// <summary>
  /// 邮件正文
  /// </summary>
  public string Body
  {
   get { return body; }
   set { if (value != body) body = value; }
  } private string body = "";
  /// <summary>
  /// 超文本格式的邮件正文
  /// </summary>
  public string HtmlBody
  {
   get { return htmlBody; }
   set { if (value != htmlBody) htmlBody = value; }
  } private string htmlBody = "";
  /// <summary>
  /// 是否是html格式的邮件
  /// </summary>
  public bool IsHtml
  {
   get { return isHtml; }
   set { if (value != isHtml) isHtml = value; }
  } private bool isHtml = false;
  public void SendMail ()
  {
   CDO.Message  oMsg  =  new  CDO.Message();  
   oMsg.To=to;
   oMsg.Subject=subject;
   oMsg.From=from;
   if(isHtml)
   {
    oMsg.HTMLBody = htmlBody;
   }
   else
    oMsg.TextBody=body;
   
            
   CDO.IConfiguration  iConfg;     
   ADODB.Fields  oFields; 
   iConfg  =  oMsg.Configuration;     
   oFields  =  iConfg.Fields; 
 
   oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value=2;   
   oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value=from;   
   oFields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value=from; 
   oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value=userName;   
   oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value=userName; 
   oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value=password; 
   oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value=1;   
   oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value=server; 
   oFields.Update(); 
        
   oMsg.Send(); 
   oMsg  =  null;

  }
 }

抱歉!评论已关闭.