我正在尝试使用以下代码发送电子邮件:
//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/