SmtpClient.Send 失败,并显示 System.FormatException:Smtp 服务器返回了无效的
本文关键字:服务器 Smtp 返回 无效 FormatException 失败 Send System 显示 SmtpClient | 更新日期: 2023-09-27 18:33:36
我正在尝试使用以下代码发送电子邮件:
//create the mail message
MailMessage mail = new MailMessage(from, to);
//set the content
mail.Subject = "This is a test email";
mail.Body = "this is the body content of the email.";
//send the message
string server = "smtp01.xyz.net";
SmtpClient smtp = new SmtpClient(server);
smtp.Credentials = new NetworkCredential(username, password);
smtp.EnableSsl = enablessl;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
Console.WriteLine(errorMessage);
}
生成的堆栈跟踪为:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.FormatException: Smtp server returned an invalid response.
at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
at System.Net.Mail.ReadLinesCommand.Send(SmtpConnection conn)
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
如果我远程登录到端口 25 上的服务器,则响应为:
220 smtp01.xyz.net ESMTP Postfix
EHLO test.com
250-smtp01.xyz.net
250-STARTTLS
250-SIZE 30000000
250-VRFY
250-ETRN
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
我已经尝试了设置和未设置的启用SSL。
我不明白它认为无效的回应是什么?
您是否有机会使用 ASP.net MVC? 如果是这样,请确保在应用程序根目录的正确 web.config 中声明/定义服务器 SMTP 服务器设置,而不是在位于"视图"文件夹中的 web.config 中声明/定义。
我遇到了这个问题,这是由于指定了一个以邮件而不是邮箱开头的服务器名称。 这个错误让我失望了,因为即使指定的SMTP服务器是问题的根源,SmtpClient的构造也没有像我尝试过的其他几台服务器那样引发异常。 错误消息使您发送的内容似乎有问题,但至少就我而言,它是指定的SMTP服务器。