我正在尝试使用以下代码发送电子邮件:
//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 上 telnet 到服务器,则响应是:
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。
我不明白它认为无效的响应是什么?