用C#(ASP.Net)在Exchange Server环境下发送邮件

普通的邮件, 用System.Net.Mail 类 或 System.Web.Mail 类 处理即可, 但是Exchange Server 环境下, 这两个类起不了作用-------至少目前我看到的情况如此.

整个过程如下:

1. 先添加COM 引用 "Microsoft CDO for Windows 2000 Library" .
2. 发送邮件的代码:

CDO.Message msg  =   new  CDO.Message();

        
string  passWord = " passWord " ;

        
string  from  =   " my@domain.com " ;

        
string  server  =   " 192.168.0.0 " ;


        msg.From 
=  from;
        msg.To 
=  from;
        msg.Subject 
=   " test mail " ;
        msg.TextBody 
=   " test. " ;

 

        CDO.IConfiguration iConfig 
=  msg.Configuration;

        ADODB.Fields fields 
=  iConfig.Fields;

 

        fields[
" http://schemas.microsoft.com/cdo/configuration/sendusing " ].Value  =   2 ;

        fields[
" http://schemas.microsoft.com/cdo/configuration/sendemailaddress " ].Value  =  from;

        fields[
" http://schemas.microsoft.com/cdo/configuration/sendpassword " ].Value  =  passWord;

        fields[
" http://schemas.microsoft.com/cdo/configuration/smtpauthenticate " ].Value  =   1 ;

        fields[
" http://schemas.microsoft.com/cdo/configuration/smtpserver " ].Value  =  server;

 

        fields.Update();

 

        
try

        
{

            msg.Send();

            msg 
=   null ;

        }


        
catch  (Exception ex)

        
{

            
throw ;

        }


 

其中, fields的这五个属性是它所有可选属性的最紧的一个子集, 即,这五个属性是缺一不可的, 其它的属性还有:
http://schemas.microsoft.com/cdo/configuration/smtpaccountname

http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress

http://schemas.microsoft.com/cdo/configuration/sendusername

等.

 

奇怪的是, msg 本身也有fields 属性, 或者如同程序中引用的: msg.Configuration.Fields, 但是如果不用上述程序中的方法, 而是直接引用 msg的Fields 或 msg.Configuration.Fields 属性, 则不能成功发送邮件, (没有错误或异常, 但是收不到邮件) 为什么会这样, 有点费解.

fields 的smtpauthenticate 属性的取值也是挺有意思的, 当它取0时, 代表无需验证, 1代表基本的验证, 2是NTLM验证, 可是我在Outlook里把验证方式设为NTLM, 而在这里把值取为1, 一样能够发送成功, 呵呵, 看来一般情况下取1 都是可以的了.

sendUsing 取2 的意思是, 强制通过smtpServer 属性指定的server 发送邮件, 所以只要server属性设置好, sendUsing取2 基本就是通用值了.

server属性设为exchange server 的IP 地址即可.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值