无法通过c#从hotmail发送电子邮件

时间:2017-03-09 12:41:20

标签: c# email smtp hotmail

当我运行此代码时,出现错误:

  

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("Error\n\n {0}", e);
            Console.ReadKey();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个;

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