发送带有附件的电子邮件使用 Cdosys.dll 库
增加 COM 引用 位置:
C:\WINDOWS\System32\cdosys.dll
控制台程序的例子如下:
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></h1>\n" +
// "</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.To = "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;
}
}
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></h1>\n" +
// "</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.To = "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;
}
}
也可以保存网页为mht文件:
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);