C # uses CDO to send mail

Source: Internet
Author: User
Tags mail reference tostring trim
Send mail Author:david Euler
Date:2004/11/18
email:de_euler-david@yahoo.com.cn
Have any questions, please contact me:

Always want to do to implement a program, regularly to send their own mail, or to friends to send messages, such as in the holiday or commemorate a few days ago, send a message to their own reminders, or friends birthday to send e-mail reminders. Looking for a long time, did not find the available information.

CSDN found that CDO could be used, and an article said, "Add CDO for Windows 2000 in reference," and then look for it in the reference, and not find a component that begins with a CDO, and look at the list of COM components that can be referenced in the afternoon. We found a COM component named Microsoft CDO for Exchange Library, which we can use to connect to SMTP Server and send mail using username/password Authentication.

Here is an example of an implementation:

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


1. Within the Explorer, add a reference (reference) to add the COM component of the Microsoft CDO for Exchange Library;

2). Edit the user interface as shown above, add Fromtextbox,totextbox,cctextbox,bcctextbox,subjecttextbox,messagetextbox,passwordtextbox in turn, Smtptextbox, set Messagetextbox TextMode property to "MultiLine", Passwordtextbox TextMode property is "Password" and add a response prompt label to add send button.

3. After entering the username, password, SMTP server, the user clicks the Send button to send the message,
The Click event code for the Send button is as follows:
Cdos. 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;

Cdos. 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" ("+") mail was sent successfully! "+" ');</script> ");
}
catch (Exception ex)
{
Response.Write ("<script>alert" ("+" Send failure: "+");</script> ");

String exmsg= "UserName:" +username+
"PASSWD:" +password+
"SMTP:" +smtptextbox.text.trim ();

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




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.