我正在尝试发送电子邮件,但返回的错误消息是:
根据验证程序
,远程证书无效
我的代码如下:
static void sendEmail()
{
var fromAddress = new MailAddress("xxxxxxxx@xxxxxxxx", "xxxxxxx");
var toAddress = new MailAddress("xxxxxxxx@xxxxxxxx", "xxxxxxxx");
const string fromPassword = "xxxxxxxxxxxx";
const string subject = "xxxxxxxxxxxxxxxxxxxxx";
var smtp = new SmtpClient
{
Host = "mailserver",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("username", fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = bodyglobal
})
{
smtp.Send(message);
}
}
答案 0 :(得分:1)
尝试设置
EnableSsl = false;
几个星期前我遇到过这个问题,这就是修复。如果您的程序不需要/使用SSL,那么smtpexecption将触发该错误消息。
答案 1 :(得分:0)
发送电子邮件取决于SMTP服务器的配置方式。
根据gmail确认你需要删除这行代码
UseDefaultCredentials = false,
只需评论此行,系统就会发送您的消息。
var smtp = new SmtpClient
{
Host = "mailserver",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
// do not use this command
// UseDefaultCredentials = false,
Credentials = new NetworkCredential("username", fromPassword)
};
Microsoft建议尽可能不使用defaultcredentials。看到这个 this