C # Send an email using CDO

xiaoxiao2021-03-06  65

Author: David Eulerdate: 2004/11 / 18Email: DE_EULER-David@yahoo.com There is any problem, please contact me :) I have always want to do a program, send mail to myself, or send a message to a friend; Send an email to yourself in the festival or commemorative day, send an email to you, or send an email on the eve of your birthday. It took a long time and did not find available information. 9CBS checks with CDO, there is an article "Add cdo for windows 2000 in Reference", so finding it in the reference, no one name is a component starting with CDO. At the afternoon, look at it. COM component list, found that there is a COM component called Microsoft CDO for Exchange 2000 Library, which is this, we can use it to connect to SMTP Server, send mail using username / password verification. Here is an example of implementation: SMTP-SRV used by SMTP Server, the login user name is David Euler, sending mailboxes is davideuler@test.com, send it to test@test.com/1). Explorer, add a reference ( Reference), add Microsoft CDO For Exchange 2000 Library COM components; 2) edit the user interface above, sequentially adding FromTextBox, ToTextBox, CCTextBox, BCCTextBox, SubjectTextBox, MessageTextBox, PasswordTextBox, smtpTextBox, provided MessageTextBox of TextMode attribute "MultiLine" The TEXTMODE attribute of PasswordTextBox is "password" and add a response prompt tab to add the send button Send. 3). Enter the username, password, and SMTP Server, the user clicks to send the message, the Send button's Click event code is as follows: cdo.Message omsg = new cdo.message (); //omsg.from = proMtextBox.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; ouerds [" http://schemas.microsoft.com/cdo/configuration/smtpauthenticate] .value = 1; Ofields ["http://schemas.microsoft.com/cdo/configuration/ SMTPSERVER "]. Value = SMTPTE Xtbox.Text.trim (); //smtp.163.com ofields.Update (); try {omsg.send (); omsg = null; response.write ("