airforce1st

导航

我比较懒,好长时间没更新,也怕写的东东不行别人不看,不过刚解决一个问题,觉得蛮有价值,就贴一下

先看我在CSDN上贴了很长时间的问题
我要做一个asp.net的web的邮件发送功能,我的方式是用户输入帐号在开机时登陆到域,然后用户打开一个web页,在web页的代码中访问域中的exchange发邮件,要求是这个时候不要让用户再输入帐号,因为这个帐号和用户开机时输入的实际上是一样的,但代码中调用exchange的确需要一个帐号,我怎么弄呢,不管是cdo还是webdav,都需要用户输入帐号,web上怎么做到象在局域网中的outlook哪样设定好后就自动存取exchange呢
在CSDN上没人答,在网上翻了好长时间也没找到方法,今天偶尔在下面这篇文章得到启发
http://www.microsoft.com/China/Community/program/originalarticles/TechDoc/impersonation.mspx
下面是我的测试代码中关键部分,希望对大家有用:用CDO调用exchange发邮件,不用输入用户名密码,自动使用当前从域登陆到IIS的身份来发邮件(exchange设为不允许匿名的)
   try
   {
    System.Security.Principal.WindowsImpersonationContext impersonationContext;
    impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
    
    CDO.Message Msg = new CDO.Message();
    Msg.From = User.Identity.Name.Substring(7);
    Msg.To = User.Identity.Name.Substring(7);
    Msg.Subject = "send from " + User.Identity.Name.Substring(7);
    Msg.HTMLBody = "<a href=\"##\">"+ User.Identity.Name.Substring(7) +"</a>";
    CDO.IConfiguration Config = Msg.Configuration;
    ADODB.Fields oFields = Config.Fields;
    oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
    oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value=0x0804;
    oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value="mail-jt";
    oFields.Update();

    Msg.BodyPart.Charset = "gb2312";
    Msg.HTMLBodyPart.Charset = "gb2312";


    Msg.Send();
    Msg = null;

    impersonationContext.Undo();
   }
   catch(Exception err)
   {
    throw err;
   }

posted on 2004-05-12 17:57  山药蛋V3.5  阅读(1763)  评论(4编辑  收藏  举报