System.Web.Mail中的验证问题

昨天想做一个遇错自动发邮件通知owner的功能,想到的是System.Web.Mail中的SmtpMail:

        MailMessage mail = new MailMessage();
        mail.Subject = “XX is going down”;
        mail.Body = “Pls check asap”;
        mail.From = “sender@xx.com”;
        mail.To = “owner1@xx.com; owner2@xx.com”; //semicolon-delimited receiver list
        mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/sendusing”, 2); //use 2 to send using SMTP over the network
        mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”, 1); //use basic authentication
        mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/sendusername”, “username”)
        mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/sendpassword”, “password”)

        try
        {
                SmtpMail.SmtpServer = “YourServer”;
                SmtpMail.Send(mail);
        }
        catch (WebException ex)
        { … }

Field的详细信息如下:
smtpserver: SMTP server name.
smtpserverport: SMTP server port (default: 25).
sendusing: cdoSendUsingPort, value 2, for sending the message using the network.
smtpauthenticate: Specifies the mechanism used when authenticating to an SMTP service over the network. Possible values are:
        – cdoAnonymous, value 0. Do not authenticate.
        – cdoBasic, value 1. Use basic clear-text authentication. When using this option you have to provide the user name and password through the sendusername and sendpassword fields.
        – cdoNTLM, value 2. The current process security context is used to authenticate with the service.
sendusername: Username
sendpassword: Password

如果当前用户的系统验证信息同样适用于邮件验证,比如各公司的域用户,则可直接使用

mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”, 2); //use current system context

不需要设置username和password

This entry was written by Longzhang Fu , posted on Thursday February 04 2010at 11:02 am , filed under .Net Framework and tagged , , . Bookmark the permalink . Post a comment below or leave a trackback: Trackback URL.

One Response to “System.Web.Mail中的验证问题”

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>