2
mailMessage.From = new MailAddress(fromEmailAddress);
        mailMessage.Subject = "Test";
        mailMessage.Body = "Manish";
        mailMessage.IsBodyHtml = true;
        mailMessage.To.Add(new MailAddress(toEmailAddress));
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 465);          
        smtp.EnableSsl = true;
        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
        NetworkCred.UserName = userNAme;
        NetworkCred.Password = password;
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Send(mailMessage);

I am trying to configure Gmail SMTP and tried with both TSL and SSL but the above code is always throwing TimeOut Exception

manlio
  • 18,345
  • 14
  • 76
  • 126
  • possible duplicate of [Sending email through Gmail SMTP server with C#](http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp) – mason May 13 '15 at 20:43

2 Answers2

0

I think you need to set usedefaultcredentials as false. Otherwise username /password that you set in network credentials will not be used.

Sanjay Singh
  • 714
  • 1
  • 8
  • 15
0

You don't want to set UseDefaultCredentials to true for the SmtpClient as it will use the credentials of the currently logged in user, as per https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.usedefaultcredentials%28v=vs.110%29.aspx

Also, it might make more sense to configure your settings via the web.config as any changes wont require recompilation and deployment of your code. Similar question answered here: Send Email via C# through Google Apps account

If you choose to keep your smtp settings in code it should look like this:

mailMessage.From = new MailAddress(fromEmailAddress);
mailMessage.Subject = "Test";
mailMessage.Body = "Manish";
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(toEmailAddress));
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 465);          
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential(userNAme, password);
smtp.Send(mailMessage);
Community
  • 1
  • 1
Brandon O'Dell
  • 1,171
  • 1
  • 15
  • 22