之前做的实现发送邮件的功能,基于System.net.mail,在本地测试是可以发送邮件的,发布到服务器上发送不了邮件,后来发现STMP默认使用25端口收发邮件,服务器封掉25了端口,导致发送邮件失败。
后来改用了587端口发送电子邮件。
public static bool SendEmail587(string email, string validateCode)
{
bool result = true;
try
{
string eMailSmtpClient = CommFunction.GetAppSetting("EMailSmtpClient"); //邮箱服务地址
string eMailAccount = CommFunction.GetAppSetting("EMailAccount");//邮箱账号
string eMailPassword = CommFunction.GetAppSetting("EMailPassword");//邮箱密码
string eMailName = CommFunction.GetAppSetting("EMailName");
string eMailSubject = CommFunction.GetAppSetting("EMailSubjectValidateCode");
string eMailBodyChinese = CommFunction.GetAppSetting("eMailBodyValidateCode_Chinese");
string eMailBodyEnglish = CommFunction.GetAppSetting("eMailBodyValidateCode_English");
MailMessage msg = new MailMessage();
msg.To.Add(email);
msg.From = new MailAddress(eMailAccount, eMailName, Encoding.UTF8);
msg.Subject = eMailSubject;
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = string.Format(string.Format("<p>{0}</p><p>{1}</p>", eMailBodyChinese, eMailBodyEnglish), validateCode); ;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(eMailAccount, eMailPassword);
client.Port = 587;
client.Host = eMailSmtpClient;
client.EnableSsl = false;
client.Send(msg);
msg.Dispose();
client.Dispose();
}
catch (Exception ex)
{
HttpRequestResult.fResultCode = HttpStatusCode.BadRequest;
HttpRequestResult.fResultMsg = ex.Message;
result = false;
}
return result;
}