我正在使用C#从本地计算机发送电子邮件,但在执行以下测试代码时遇到错误。
C# 代码:
protected void SendEmail(string emailAddres)
{
SmtpClient smtp = new SmtpClient();
MailMessage mail = new MailMessage()
{
To = { new MailAddress(emailAddres) },
From = new MailAddress("email@email.com", "fromEmail"),
Subject = "Subject of a test email!",
IsBodyHtml = true,
Body = "Test body email.",
BodyEncoding = System.Text.Encoding.UTF8,
SubjectEncoding = System.Text.Encoding.UTF8
};
try
{
smtp.Send(mail);
lblResultEmail.Text = "Message Sent Succesfully";
lblResultEmail.ForeColor = System.Drawing.Color.Green;
}
catch (Exception ex)
{
lblResultEmail.Text = ex.ToString();
lblResultEmail.ForeColor = System.Drawing.Color.Red;
}
}
在 Web.config 文件中,我添加了以下内容:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.office365.com" port="587" defaultCredentials="false" enableSsl="true"
userName="email@email.com" password="ThisPwd" targetName="STARTTLS/smtp.office365.com"
/>
</smtp>
</mailSettings>
</system.net>
错误信息:
System.Net.Mail.SmtpException:SMTP 服务器要求安全连接,或客户端未进行身份验证。服务器响应为:5.7.57 SMTP;在MAIL FROM期间未经身份验证的客户端无法发送匿名邮件[AM0PR05CA0077.eurprd05.prod.outlook.com] at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at...
编辑:
这可能是通过Office 365发送电子邮件时出现SMTP 5.7.57错误的重复问题。但我在那里找不到解决方案,因此我想重新打开讨论。