-3

I wrote this code and It's not working

try
    {
        MailMessage mailMessage = new MailMessage();
        mailMessage.To.Add("XXXXXXX@gmail.com");
        mailMessage.From = new MailAddress("XXXXXXX@gmail.com");
        mailMessage.Subject = "ASP.NET e-mail test";
        mailMessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
        smtpClient.Send(mailMessage);
        Response.Write("E-mail sent!");
    }
    catch (Exception ex)
    {
        Response.Write("Could not send the e-mail - error: " + ex.Message);
    }

I Took this code from asp.net tutorials. It is not working for me.

I get an error:

Failed to send e-mail

Appreciate any help

durron597
  • 31,968
  • 17
  • 99
  • 158

1 Answers1

0

Pass the port number .

new SmtpClient("smtp.gmail.com", 587);
Yousuf
  • 3,105
  • 7
  • 28
  • 38