SMTP电子邮件发送请求有时会失败 [英] smtp email send request fails sometimes
问题描述
我正在c#中使用以下代码,
I am using below code in c#,
string Subject = "test12";
MailMessage mail = new MailMessage();
mail.To.Add(item.ToString());
mail.From = new MailAddress(EmailUserName);
mail.Subject = Subject;
mail.Body = PopulateBody();
mail.IsBodyHtml = true;
SmtpClient client = new SmtpClient(EmailHost, EmailPort);
client.EnableSsl = true;
NetworkCredential credentials = new NetworkCredential(EmailUserName, EmailPassword);
client.UseDefaultCredentials = false;
client.Credentials = credentials;
client.Send(mail);
Client.send(mail)方法出现错误
I am getting error in Client.send(mail) method
我尝试过的事情:
System.Security.Authentication.AuthenticationException:对SSPI的调用失败,请参阅内部异常.---> System.ComponentModel.Win32Exception:不支持所请求的功能---内部异常堆栈跟踪的结尾---在System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken消息,AsyncProtocolRequest asyncRequest,Exception异常)
System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The function requested is not supported --- End of inner exception stack trace --- at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception) at
推荐答案
我会先尝试不通过身份验证,以检查是否提供了其他错误,然后尝试不使用ssl.
I would try without authentication first to check if that gives a different error and also try without ssl.
protected bool NotifyByMail(string server, string strFrom, string strTo, string strSubject, string strBodyText, bool isBodyTextHtml = false)
{
if (string.IsNullOrEmpty (server)
|| string.IsNullOrEmpty (strFrom)
|| string.IsNullOrEmpty (strTo)
|| string.IsNullOrEmpty (strSubject)
|| string.IsNullOrEmpty (strBodyText))
return false;
try {
MailAddress from = new MailAddress (strFrom);
MailAddress to = new MailAddress (strTo);
MailMessage message = new MailMessage (from, to);
message.Subject = strSubject;
message.Body = strBodyText;
message.IsBodyHtml = isBodyTextHtml;
SmtpClient client = new SmtpClient (server);
// Include credentials if the server requires them.
//client.Credentials = new System.Net.NetworkCredential ("********", "*******");// System.Net.CredentialCache.DefaultNetworkCredentials;
client.Send (message);
return true;
}
catch (Exception exception) {
// TODO ErrorHandling
}
return false;
}
这篇关于SMTP电子邮件发送请求有时会失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!