郵件發送(c#)小試

1、添加參考:CDO(在com中Microsoft CDO for Windows 2000 Libray下)

2、設定email server(例子為126的email發送。其它的以實際情況修改)

//第一種方法

CDO.MessageClass Msg = new MessageClass();
CDO.ConfigurationClass conf = new ConfigurationClass();

conf.Fields[CDO.CdoConfiguration.cdoSMTPServer].Value = "smtp.126.com";
conf.Fields[CDO.CdoConfiguration.cdoSendUsingMethod].Value = CDO.CdoSendUsing.cdoSendUsingPort;
conf.Fields[CDO.CdoConfiguration.cdoSMTPAuthenticate].Value = CDO.CdoProtocolsAuthentication.cdoBasic;
conf.Fields[CDO.CdoConfiguration.cdoSendUserName].Value = "[email protected]";
conf.Fields[CDO.CdoConfiguration.cdoSendPassword].Value = "emailpassword";

conf.Fields.Update();

//第二種方法

CDO.MessageClass Msg = new MessageClass();
CDO.ConfigurationClass conf = new ConfigurationClass();
ADODB.Fields Flds = conf.Fields;

Flds["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "smtp.126.com";
Flds["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;

Flds["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1;
Flds["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = "[email protected]";
Flds["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = "emailpassword";

Flds.Update();

3、發送email

Msg.Configuration = conf;
Msg.To = "[email protected]";//可以用;分開發多個
Msg.From = "[email protected]";
Msg.Subject = "test。。.";

Msg.BCC = "";//暗(密)送
Msg.CC = "";//抄送
Msg.AddAttachment("E:\\test.JPG", "", "");//附件1
Msg.AddAttachment("E:\\test.txt", "", "");//附件2

Msg.TextBody = "susan test";//or mailMsg.HTMLBody = "<html><body>sssss</body></html>";

//send
try {
Msg.Send();
}
catch(Exception ex)

{
throw ex;
}

你可能感兴趣的:(C#)