Send mail in the Exchange Server environment with C # (ASP. NET)

Source: Internet
Author: User

Normal mail, with the System.Net.Mail class or the System.Web.Mail class, but in an exchange Server environment, these two classes do not work-------at least for now I see.

The whole process is as follows:

1. Add the COM reference "Microsoft CDO for Windows Library" first.
2. The code to send the message:

 CDO. Message msg = new   CDO.        Message ();  string  password= "  password   " string  from  = " Span style= "Color:rgb (128, 0, 0); > " [email protected]           string  server =  " Span style= "Color:rgb (128, 0, 0); >192.168.0.0   " ;// Send 
using HTML format

MsgLync.BodyPart.Charset = "gb2312"; Encoding to ensure that content is not garbled
Msglync.htmlbody = "<div style= ' color:red ' > How are you? Automatically send mail from the server </div> ";

Msg. from= from; Msg. to= from; Msg. Subject="Test Mail"; Msg. TextBody="test."; Cdos. IConfiguration IConfig=Msg.        Configuration; ADODB. 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; Where the five properties of fields are the most compact subset of all of its optional properties, that is, these five attributes are integral, and other properties are: http://Schemas.microsoft.com/cdo/configuration/smtpaccountnamehttp://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddresshttp://Schemas.microsoft.com/cdo/configuration/sendusernameand so on.

Strangely, MSG itself has the fields attribute, or as it is referenced in the program: Msg. Configuration.fields, but if you do not use the method in the above program, instead directly refer to MSG's fields or MSG. Configuration.fields property, the message cannot be sent successfully (no error or exception, but cannot receive the message) Why is this, a little confusing.

The value of the Smtpauthenticate property of fields is also interesting, when it takes 0 o'clock, represents no validation, 1 for Basic authentication, 2 for NTLM authentication, but I set the authentication method to NTLM in Outlook, and here the value is 1, the same can be sent successfully, Oh, it seems that the general situation to take 1 is possible.

SendUsing 2 means that forcing a message from the server specified by the SmtpServer property is mandatory, so as long as the server property is set, sendusing Fetch 2 is basically a common value.

The server property is set to the IP address of Exchange server.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.