在异步发送电子邮件时会产生错误。 它适用于smtp设置。问题在于域特定的主机名。 无法识别此问题。 以下是我的代码段:
private static async void SendEmail(LogFileType logType, SendLogJob job)
{
#region EMAIL_CONSTANTS
string From = "abc@company.com";
string To = "username@domain.com";
#endregion
try
{
MailAddress fromUsername = new MailAddress(From);
MailAddress from = new MailAddress(From, "Auto Generated Mail", System.Text.Encoding.UTF8);
MailAddress to = new MailAddress(To);
const string fromPassword = "1234";
var smtp = new SmtpClient
{
Host = "mail3.gridhost.co.uk",
Port = 465,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromUsername.Address, fromPassword)
};
var message = new MailMessage(from, to)
{
Subject = "Subject of the Email goes here",
Body = "This is the EMail Body",
IsBodyHtml=true,
SubjectEncoding = System.Text.Encoding.UTF8,
BodyEncoding = System.Text.Encoding.UTF8
};
smtp.SendCompleted += (s, e) =>
{
SendCompletedCallback(s, e);
smtp.Dispose();
message.Dispose();
};
MemoryStream stream = new MemoryStream(new byte[64000]);
Attachment attachment = new Attachment(stream, logType + " File");
message.Attachments.Add(attachment);
smtp.SendAsync(message, message);
}
catch (Exception ex)
{
ex.Message.ToString();
}
它抛出错误:
Syntax error, command unrecognized. The server response was:
状态代码:0
堆栈追踪:
at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result) at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)
任何人都可以帮我解决这个问题.. !!
答案 0 :(得分:0)
已解决..刚刚更改了端口号。到587