Sakshi Jain
Sakshi Jain

Reputation: 11

Not able to send email using SMTP through my .net MVC web application

MailMessage mail = new MailMessage();
mail.To.Add("[email protected]");
mail.From = new MailAddress("[email protected]");
mail.Subject = "test";
string Body = "test";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 495; //Also tried with 587
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "passwordOfSome_id");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Send(mail);

I am trying to run this code on my localhost. This code is written in my MVC3 web application hosted on IIS7. I am getting this SMTP exception -

Failure sending mail (Message=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 173.194.66.109:587.

I have tried swapping the from and to addresses also. Please help!


Upvotes: 1

Views: 529

Answers (1)

jjardine
jjardine

Reputation: 11

You might want to try enabling support for "less secure apps" in your gmail account, because your code looks fine. Although I'd stick to port 587.

See here: https://www.google.com/settings/security/lesssecureapps

Upvotes: 1

Related Questions