.NET

It might surprise you to find out that sending email using Gmail SMTP can sometimes create an issue for developers and cause them to spend lot of time finding a solution. If you have encountered this issue, there are few important verifications required before you can resolve the problem.

1. UserDefaultCredentials = False, must always appear before Credentials = new System.Net.NetworkCredential. If you insert this below Credentials, it will clear the authentication object and wait for new authentication parameters. Add UserDefaultCredentials before supplying authentication credentials and you will solve this problem.

2. Gmail supports two PORTS for sending an email, 587 and 465. If you trying to send using System.Net.Mail, it will not work with PORT 465, because System.NET.Mail only supports Explicit SSL. You can get more details on Explicit and Implicit SSL from http://blogs.msdn.com/b/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx, if you need more information. Use port 587, rather than 465 so you do not experience an error, like connection timeout etc.

3. If you have a Google premium account, e.g., name@yourdomain.com, and you don’t have enough rights to allow other applications to send email using your email ID and password, you will need to employ a 2-step verification process so that other apps can send email using your authentication. You should also consider contacting your Administrator to get permission to establish and change settings and allow other application to send email with your authentication. If premium account authentication doesn’t work try a gmail ID like this: name@gmail.com. That should do the trick.

###

Take a look at the C# Code below and use it to resolve the above-described issues. It’s a great shortcut!

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);
try
{
m.From = new MailAddress("FROM EMAIL", "FROM NAME");
m.To.Add(new MailAddress("TO EMAIL", "TO NAME"));
m.Subject = " This is a Test Mail new";
m.IsBodyHtml = true;
m.Body = "Phone number - ";
sc.UseDefaultCredentials = false;
sc.Credentials = new System.Net.NetworkCredential("GMAIL_EMAILID", "GMAIL_EMAILID_PASSWORD");
sc.EnableSsl = true;
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.Timeout = 120000;
sc.Send(m);
Response.Write("Email Send sucessfully");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}

There is always more than one way to resolve a problem, and sometimes the easiest, fastest way is to reach out to the developer community and see what others are doing. Our business is one that requires constant learning and skill development and, thankfully, is always someone who can help!