我正在尝试运行此代码,但出现以下异常: 发送邮件失败。
我的代码如下:
try
{
MailMessage mail = new MailMessage("from@gmail.com", "to@gmail.com");
SmtpClient client = new SmtpClient
{
Port = 465,
UseDefaultCredentials = false,
Host = "smtp.gmail.com",
EnableSsl = true,
Credentials = new NetworkCredential("from@gmail.com", "xxxxxxx")
};
mail.Subject = "Hello";
mail.Body = "This is test Message";
client.Send(mail);
}
catch (Exception _ex)
{
Debug.Write(_ex.Message);
}
即使我打开了“安全性较低的应用程序”
答案 0 :(得分:0)
假设您的凭据是正确的,则初始化可能有误。
这是我的摘录
SmtpClient client = new SmtpClient();
MailMessage mailMessage = new MailMessage();
client = new SmtpClient("smtp.xxx.com", port_number);
client.Credentials = new NetworkCredential("xxx@xxx.xx", "xxx");
client.EnableSsl = true;
mailMessage.From = new MailAddress("from@xxx.co", "xx xx", Encoding.UTF8);
mailMessage.Subject = $"mail subject";
mailMessage.Body = emailHtml;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add("to@xx.com");
mailMessage.Bcc.Add("bcc@xx.com");
await client.SendMailAsync(mailMessage);