我有下面的代码,它无法向任何电子邮件地址发送电子邮件,它总是返回错误的结果,此代码之前有效,当我们更改网络服务器时停止工作,我需要安装任何额外的软件吗?
public bool sendEmail(string to,string from,string message,string subject,string reply_to,string cc)
{
MailMessage msgMail = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
MailAddress mAdd = new MailAddress(from);
MailAddress mAddTo = new MailAddress(to);
System.Net.NetworkCredential nc = new System.Net.NetworkCredential();
nc.UserName = System.Configuration.ConfigurationManager.AppSettings["MailServerID"].ToString();
nc.Password = System.Configuration.ConfigurationManager.AppSettings["MailServerPASS"].ToString();
if (reply_to != "")
{
MailAddress mAddRTo = new MailAddress(reply_to);
msgMail.ReplyTo = mAddRTo;
}
if(cc!="")
msgMail.CC.Add(cc);
// msgMail.Headers.Add("Date ", DateTime.Now.ToString());
msgMail.To.Add(mAddTo);
msgMail.From = mAdd;
msgMail.Subject = subject;
msgMail.IsBodyHtml = true;
msgMail.Body =message;
smtpClient.Host = System.Configuration.ConfigurationManager.AppSettings["MailServer"].ToString();
smtpClient.Credentials = nc;
try
{
smtpClient.Send(msgMail);
return true;
}
catch (Exception e)
{
return false;
}
}
我的 web.config smtp 配置;
<appSettings>
<add key="MailServer" value="smtp.gmail.com"/>
<add key="MailServerID" value="abcd@gmail.com"/>
<add key="MailServerPASS" value="XXXXXXX"/>
</appSettings>