在SmtpMail.Send(message);之前加上以下几句:
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",username); //message.From
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",password); //message.From的密码
SmtpMail.SmtpServer=smtpServer;//如:"smtp.126.com"; 必须与message.From 的邮箱一致。
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",username); //message.From
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",password); //message.From的密码
SmtpMail.SmtpServer=smtpServer;//如:"smtp.126.com"; 必须与message.From 的邮箱一致。
using System.Runtime.InteropServices;
using CDO;private void SendMail()
{
System.DateTime dtNow = System.DateTime.Now;
MailAttachment ac = new MailAttachment("Data.xls");
MailMessage Message = new MailMessage(); lblTips.Text = "正在发送邮件...";
lblTips.Left = (this.Width - lblTips.Width) / 2;
lblTips.Refresh(); try
{
Configuration conf = new ConfigurationClass(); conf.Fields[CdoConfiguration.cdoSendUsingMethod].Value = CdoSendUsing.cdoSendUsingPort;
conf.Fields[CdoConfiguration.cdoSMTPServer].Value = "***.***.***.***";
conf.Fields[CdoConfiguration.cdoSMTPServerPort].Value = 25;
conf.Fields[CdoConfiguration.cdoSMTPAccountName].Value = "Account";
conf.Fields[CdoConfiguration.cdoSendUserReplyEmailAddress].Value = "\"Account\" <[email protected]>";
conf.Fields[CdoConfiguration.cdoSendEmailAddress].Value = "\"Account\" <[email protected]>";
conf.Fields[CdoConfiguration.cdoSMTPAuthenticate].Value = CdoProtocolsAuthentication.cdoBasic;
conf.Fields[CdoConfiguration.cdoSendUserName].Value = "Account";
conf.Fields[CdoConfiguration.cdoSendPassword].Value = "Password"; conf.Fields.Update(); MessageClass msg = new MessageClass();
msg.Configuration = conf; msg.AddAttachment(System.Windows.Forms.Application.StartupPath+"\\Data.xls","","");
msg.To = "MailTo";
msg.From = "MailFrom";
msg.Subject = dtNow.Year.ToString() + "年" + dtNow.Month.ToString() + "月";
msg.HTMLBody = "请查收附件!"; msg.Send(); lblTips.Text = "邮件发送完成...";
lblTips.Left = (this.Width - lblTips.Width) / 2;
lblTips.Refresh();
}
catch (System.Runtime.InteropServices.COMException eLoad)
{
lblTips.Text = "邮件发送失败...";
lblTips.Left = (this.Width - lblTips.Width) / 2;
lblTips.Refresh();
}
}
MailAttachment ac = new MailAttachment("Data.xls");hoho~~~,不好意思~~~