using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
我运行该程序,他给我带来了一个错误无法提交邮件..这是代码可能是什么问题?
答案 0 :(得分:3)
尝试以下方法:
using (var client = new SmtpClient("smtp.gmail.com", 587))
{
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("username", "password");
var message = new MailMessage(
"sender@gmail.com",
"recipient@domain.com",
"some subject",
"mail body"
);
client.Send(message);
}
另外,请确保您不在代理服务器后面,并且运行此程序的计算机可以访问smtp.gmail.com
,并且防火墙不会阻止端口587。