C # Use CDO to send emails

Source: Internet
Author: User
The list of COM components that can be referenced shows that there is a COM component named Microsoft CDO for Exchange 2000 library, which we can use to connect to the SMTP server, use the user name/password to send the email.

The following is an example of implementation:

SMTP server uses SMTP-SRV, login user name is David Euler, send mailbox is davidEuler@test.com, send to test@test.com/

1) In resource manager, add reference and the COM component of Microsoft CDO for Exchange 2000 library;

2 ). edit the user interface, such as fromtextbox, totextbox, cctextbox, bcctextbox, subjecttextbox, messagetextbox, passwordtextbox, smtptextbox, and set the textmode attribute of messagetextbox to "multiline". the textmode attribute of passwordtextbox is "password", add the response prompt tag, and add the send button.

3) after entering the user name, password, and SMTP server, the user clicks the send button to send the email,
The Click Event code of the send button is as follows:

CDO. Message omsg = new CDO. Message ();
// Omsg. From = fromtextbox. text;
Omsg. To = totextbox. text;
Omsg. Subject = subjecttextbox. text;
Omsg. textbody = messagetextbox. text;
Omsg. Cc = cctextbox. text;
Omsg. bcc = bcctextbox. text;
String username;
String emailfrom;
String Password = passwordtextbox. Text. tostring (). Trim ();

Username = fromtextbox. Text. Trim ();
Emailfrom = username. Replace ("", "") + "@ test.com ";
Omsg. From = emailfrom;

CDO. iconfiguration iconfg;
ADODB. Fields ofields;
Iconfg = omsg. configuration;
Ofields = iconfg. fields;

Ofields ["http://schemas.microsoft.com/cdo/configuration/sendusing"]. value = 2;
Ofields ["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"]. value = emailfrom;
Ofields ["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"]. value = emailfrom;
Ofields ["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"]. value = username;
Ofields ["http://schemas.microsoft.com/cdo/configuration/sendusername"]. value = username;
Ofields ["http://schemas.microsoft.com/cdo/configuration/sendpassword"]. value = password;
Ofields ["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]. value = 1;
Ofields ["http://schemas.microsoft.com/cdo/configuration/smtpserver"]. value = smtptextbox. Text. Trim (); // smtp.163.com
Ofields. Update ();

Try
{
Omsg. Send ();
Omsg = NULL;
Response. Write ("<SCRIPT> alert ('" + "message sent successfully! "+" '); </SCRIPT> ");
}
Catch (exception ex)
{
Response. Write ("<SCRIPT> alert ('" + "failed to send:" + "'); </SCRIPT> ");

String exmsg = "username:" + username +
"Passwd:" + password +
"SMTP:" + smtptextbox. Text. Trim ();

Response. Write ("<SCRIPT> alert ('" + exmsg + "'); </SCRIPT> ");
Failedlabel. Text = ex. Message. tostring ();
}......


 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1539991

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.