我正在尝试使用默认凭据和SMTPClient发送电子邮件。
我得到的例外是:
SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.7.1客户端未通过身份验证。
我要使用的代码:
public void SendEmail(List<string> recipients, string subject, string body)
{
if (recipients.Count == 0)
return;
MailMessage mail = new MailMessage();
SmtpClient client = new SmtpClient();
foreach (string to in recipients)
{
mail.To.Add(to);
}
mail.From = new MailAddress("email@email.com");
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.Host = "smtprelay.email.com";
//client.Credentials = new System.Net.NetworkCredential("email@email.com", password);
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
client.EnableSsl = true;
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
try
{
client.Send(mail);
}
catch(Exception e)
{
Console.Write(e.Message);
}
mail.Dispose();
}
我使用EnableSsl = true和false进行了测试,但均无效果。如果我将UseDefaultCredentials设置为false,并通过NetworkCredential给予它们,那么它将起作用。
是否有需要交换的设置或需要设置的设置?
编辑:int ServerCertificateCallback,我得到: '(((System.Net.Mail.SmtpClient)s).ServicePoint.Address'引发了类型为'System.NotSupportedException'的异常
和sslPolicyErrors是System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch
这是否意味着组策略不允许这样做?
答案 0 :(得分:0)
Exchange需要一个有效的域帐户(登录/密码)或您的邮局主管已配置为允许的IP地址。
答案 1 :(得分:0)
您可以在下面尝试此代码
public static void logMail(string err)
{
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential("email@email.com", "password");
smtp.Port = 25;
smtp.Host = "mail.email.com";
MailAddress from = new MailAddress("email@email.com", "test");
MailAddress to = new MailAddress("vb_error@email.com");
MailMessage mail = new MailMessage(from, to);
mail.IsBodyHtml = true;
mail.Subject = "Error";
mail.Body = "<b>Error Message:</b> " + err;
smtp.Send(mail);
}