Use the cdosys. dll library to send emails with attachments

Source: Internet
Author: User

Add com reference location:

C: \ windows \ system32 \ cdosys. dll

ConsoleProgramExample:

Using System;
Class Class1
{
Static   Void Main ( String [] ARGs)
{
Try  
{
CDO. Message omsg =   New CDO. Message ();
CDO. iconfiguration iconfg;

Iconfg=Omsg. configuration;

ADODB. Fields ofields;
Ofields=Iconfg. fields;

// Set Configuration.
ADODB. Field ofield = Ofields [ " Http://schemas.microsoft.com/cdo/configuration/sendusing " ];

// Todo: to send by using the smart host, uncomment the following lines:
// Ofield. value = CDO. cdosendusing. cdosendusingport;
// Ofield = ofields [" Http://schemas.microsoft.com/cdo/configuration/smtpserver "];
// Ofield. value = "smarthost ";

// Todo: to send by using local SMTP service.
// Ofield = ofields [" Http://schemas.microsoft.com/cdo/configuration/sendusing "];
// Ofield. value = 1;

Ofields. Update ();

//Set common properties from message.

//Todo: to send text body, uncomment the following line:
//Omsg. textbody = "Hello, how are you doing? ";

// Todo: to send HTML body, uncomment the following lines:
// String shtml;
// Shtml = "<HTML> \ n" +
// "<Head> \ n" +
// "<Title> sample GIF </title> \ n" +
// "</Head> \ n" +
// "<Body> <p> \ n" +
// "<H1> <font color = green> inline graphics </font> // "</Body> \ n" +
// "</Html> ";
// Omsg. htmlbody = shtml;

// Toto: to send web page in an e-mail, uncomment the following lines and make changes in todo section.
// Todo: Replace with your preferred web page
// Omsg. createmhtmlbody (" Http://www.microsoft.com ",
// CDO. cdomhtmlflags. cdosuppressnone,
// "","");
Omsg. Subject =   " Test SMTP " ;

// Todo: Change the to and from address to reflect your information.
Omsg. From =   " Someone@example.com " ;
Omsg. =   " Someone@example.com " ;
// Add attachment.
// Todo: change the path to the file that you want to attach.
Omsg. addattachment ( " C: \ hello.txt " , "" , "" );
Omsg. addattachment ( " C: \ test.doc " , "" , "" );
Omsg. Send ();
}
Catch (Exception E)
{
Console. writeline ( " {0} exception caught. " , E );
}
Return ;
}
}

You can also save the webpage as an MHT file:

CDO. Message MSG = new CDO. messageclass ();
CDO. Configuration CFG = new CDO. configurationclass ();

MSG. Configuration = CFG;
MSG. createmhtmlbody("Http://www.sina.com.cn ",CDO. cdomhtmlflags. cdosuppressall ,"","");

MSG. getstream (). savetofile("C: \ A. MHT ",ADODB. saveoptionsenum. adsavecreateoverwrite );

 

 

 

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.