0
votes

When I run this code, I get an error :

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 65.55.163.152:587

Code :

static void Main(string[] args) 
{
    string smtpAddress = "smtp.live.com";
    int portNumber = 587;
    bool enableSSL = true;
    string emailFrom = "[email protected]";
    string password = "xxxxxxxxxxx";
    string emailTo = "[email protected]";
    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
are you sure there are no firewalls between you and smtp.live.com?BugFinder

1 Answers

0
votes

Try this;

 using System.Net.Mail;

...

MailMessage mail = new MailMessage("[email protected]", "[email protected]");
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);