当我运行此代码时,我会收到一个错误:
system.net.mail.smtpexception:失败发送邮件。---> System.net.WebException:无法连接到远程服务器 ---> system.net.sockets.socketException:连接尝试失败,因为连接方在一段时间后没有正确响应 时间或建立连接失败,因为连接的主机有 无法响应65.55.163.152:587
代码:
static void Main(string[] args)
{
string smtpAddress = "smtp.live.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "xxxxx@hotmail.co.uk";
string password = "xxxxxxxxxxx";
string emailTo = "myname@businessemail.co.uk";
string subject = "Daily Email Check";
string body = "Email reached business exchange server from an external hotmail email account";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = false;
try
{
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
catch (Exception e)
{
Console.WriteLine("Errornn {0}", e);
Console.ReadKey();
}
}
}
尝试这个;
using System.Net.Mail;
...
MailMessage mail = new MailMessage("xxxxx@hotmail.co.uk", "myname@businessemail.co.uk");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.live.com";
mail.Subject = "Daily Email Check";
mail.Body = "Email reached business exchange server from an external hotmail email account";
client.Send(mail);