【发布时间】:2021-03-21 00:22:26
【问题描述】:
我在尝试执行此邮件发送方法时收到异常。
{"语法错误,命令无法识别。服务器响应为:"}
我的代码:
public async static Task SendExceptionMail(Exception e)
{
try
{
//TODO: fill..
var message = new MailMessage();
message.To.Add("other_email_than_my_email@gmail.com");
message.From = new MailAddress("my_email_in_gmail@gmail.com");
message.Subject = "Server Exception Occured";
StringBuilder sb = new StringBuilder();
sb.AppendLine("Exception occured. Stack trace:");
sb.AppendLine(e.StackTrace);
sb.AppendLine("");
sb.AppendLine("Time: " + DateTime.UtcNow);
message.Body = sb.ToString();
message.IsBodyHtml = false;
message.BodyEncoding = UTF8Encoding.UTF8;
using (var smtpClient = new SmtpClient())
{
smtpClient.Credentials = new System.Net.NetworkCredential("my_email_in_gmail@gmail.com", "very_very_complicated_password_with_numbers_and_signs");
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 465;
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
//smtpClient.UseDefaultCredentials = false;
await smtpClient.SendMailAsync(message);
}
}
catch (Exception ex)
{
Console.Write(ex.StackTrace);
}
}
在我的 Gmail 帐户中,我在设置选项卡中允许 IMAP 和 POP。
我尝试过的:
- 将端口更改为 587 和 25。这次我收到了
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at - 注释/取消注释
UseDefaultCredentials行,DeliveryMethod属性 - 注释/取消注释
IsBodyHtml和BodyEncoding属性
【问题讨论】: