Jasper Catthoor
Jasper Catthoor

Reputation: 625

C# smtp.google.com could not be resolved

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.

Upvotes: 6

Views: 20801

Answers (2)

Ali Mahdian
Ali Mahdian

Reputation: 1

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

Upvotes: 0

Oluwafemi
Oluwafemi

Reputation: 14899

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

Upvotes: 18

Related Questions