62,046
社区成员
发帖
与我相关
我的任务
分享
public static bool SendMail(Dictionary<string, Dictionary<string, string>> config, string subject, string body, Encoding encoding,bool cdo) {
Dictionary<string, string> addresses = config["to"];
foreach (string e in addresses.Keys) {
CDO.Message objMail = new CDO.Message();
try {
//发件人
string address = config["from"]["address"] as string;
string host = config["smtp"]["host"] as string;
string userName = config["smtp"]["user"] as string;
string password = config["smtp"]["password"] as string;
objMail.To = addresses[e];
objMail.From = address;
objMail.Subject = subject;
objMail.HTMLBody = body;//邮件内容
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 465;//设置端口
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = host;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = userName;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value = userName;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = userName;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = userName;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = password;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"].Value = "true";//这一句指示是否使用ssl
objMail.Configuration.Fields.Update();
objMail.Send();
} catch (Exception ex) { throw ex; } finally {
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(objMail);
objMail = null;
}
return true;
}