Use C # (ASP. Net) to send emails in the Exchange Server Environment

Source: Internet
Author: User

For common emails, use System. net. mail class or System. web. the Mail class can be processed, but in the Exchange Server environment, these two classes do not work ------- at least what I have seen so far.
The process is as follows:
1. First add COM reference "Microsoft CDO for Windows 2000 Library ".
2. Email sending code:
 
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;
 
}
 
 
 
Among them, the five attributes of fields are the most restrictive subset of all its optional attributes, that is, these five attributes are indispensable, and other attributes include:
Http://schemas.microsoft.com/cdo/configuration/smtpaccountname
Http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress
Http://schemas.microsoft.com/cdo/configuration/sendusername
And so on.
 
The strange thing is that msg itself also has the fields attribute, or as referenced in the program: msg. configuration. fields. configuration. the Fields attribute does not allow you to send emails successfully (no errors or exceptions, but no emails can be received). Why.
The value of the smtpauthenticate attribute of fields is also very interesting. When it is set to 0, it indicates that verification is not required, 1 indicates basic verification, and 2 indicates NTLM verification, however, in Outlook, I set the verification method to NTLM, and here I set the value to 1, which can be sent successfully.
SendUsing 2 indicates that an email is sent by the server specified by the smtpServer attribute. Therefore, if the server attribute is set, sendUsing 2 is a common value.
Set the server property to the IP address of the exchange server.

 

From Author: Xia Lang zai
 

Related Article

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.