6

Following code used to work but suddenly refuses to work.

private static void SendMail()
{
    try
    {
        var mail = new MailMessage();
        var smtpServer = new SmtpClient("smtp.google.com", 587);
        mail.From = new MailAddress("[email protected]", "Jasper.Kattoor");
        mail.To.Add("YYYY");
        mail.Subject = "sup";
        mail.Body = "sup";
        smtpServer.Credentials = new NetworkCredential("[email protected]", "XXXX");
        smtpServer.EnableSsl = true;
        smtpServer.Send(mail);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
        Console.ReadLine();
    }
}

I receive the following error:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'smtp.google.com'

I've also tried using hotmail instead of gmail, same error. I can still send mails manually though. Why would this error suddenly occur? Yesterday there were no problems with this.

2 Answers 2

18

That remote host name is wrong, it should be:

smtp.gmail.com

Read all about it: Send Email from Yahoo!, GMail, Hotmail (C#)

Updates: You can also ping the host name to check if it exists using command prompt

enter image description here

3
  • 1
    Oh god how could I not have seen this. Thank you. Still wondering what happened since it used to work.. Commented Aug 19, 2015 at 7:49
  • 3
    The smallest bugs causes the biggest headaches :)
    – Oluwafemi
    Commented Aug 19, 2015 at 7:55
  • 1
    i don't have both smtp.gmail.com and smtp.google.com what should i do now? Same error
    – Af'faq
    Commented May 10, 2017 at 9:03
0

Yes, in my case I wasn't just connected to the internet. After I connected the problem was gone.

1
  • This should be a comment.
    – Mansoor
    Commented Sep 12, 2020 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.