c# - SmtpClient.Send 失败,出现 System.FormatException : Smtp server returned an invalid response

标签 c# .net

我正在尝试使用以下代码发送电子邮件:

//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)

如果我 telnet 到端口 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

我尝试过设置和未设置 EnableSsl。

我不明白它认为无效的响应是什么意思?

最佳答案

您是否有机会使用 ASP.net MVC?如果是这样,请确保您的服务器 SMTP 服务器设置在应用程序根目录的正确 web.config 中声明/定义,而不是在位于 Views 文件夹中的 web.config 中声明/定义。

关于c# - SmtpClient.Send 失败,出现 System.FormatException : Smtp server returned an invalid response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10523773/

相关文章:

c# - 删除不属于不同字符串的某些字符串?

c# - C# 和 .NET 中的 libcurl

c# - 将属性名称存储在公共(public)常量字符串中是一种好习惯吗?

c# - 在 .NET 中粉碎文件

.net - 如何克服对 ASP.Net MVC 标记中的 <% %> 的恐惧?

c# - .NET 中的 GO 语句炸毁了 sql 执行

c# - 在 C# 中执行不同事件的多个按键

c# - CaSTLe Windsor Ms Adapter Core 2.0 实现

c# - HttpClient.PostAsJsonAsync 永远看不到帖子何时成功并响应

c# - 如何在 HTMLGenericControl 上注册服务器端点击?