Tuesday, November 18, 2014

Sending email with smtp.gmail.com in ASP.Net MVC


Recently I started working on sending an email from an ASP.Net MVC application, while checking on the internet everybody is giving the base code for sending an email. I was trying to use smtp.google.com as my SMTP server with port 587. I was always getting an error server actively rejected my connection. Nobody is giving concrete information on enabling settings on gmail account settings. I used to get an email from Gmail saying un-authorized access to my account is blocked. And the email has a link to allow less secure app settings, which allows us to login from different devices and applications using our own settings. You need to enable to send email from your application.

Here is the URL for gmail settings: Gmail Settings

Also verify settings for Antivirus installed on your machine. By default, it will block the connection. You can verify the same in Event Viewer -> Application logs - look for any Warning/Information messages related to this connection. You may find some thing like this:
Blocked by port blocking rule (Anti-virus Standard Protection:Prevent mass mailing worms from sending mail). And a warning message showing the same message as the exception you got from the code.

You need to uncheck the setting for "Prevent mass mailing worms from sending mail" in you Anti-virus software. Here is the snapshot for the same for McAfee.

Please refer to anti-virus settings as the settings may differ based on the Anti-virus installed on your machine. If Anti-virus is blocking the connection you may the following exception form your code

No connection could be made because the target machine actively refused it 74.125.207.109:587

If Gmail setting is disabled you may get the following exception from your code

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at


Here is an example code I used to send email using Google SMTP server after enabling my Gmail settings for my account and updating Anti-virus settings on my machine.

To send content, you need to use AlternativeView.

Namespace: System.Net.Mail

Create mail message:


var mailMessage = new MailMessage
{
From = new MailAddress("user@gmail.com", "firstname, lastname"),
Subject = "Test message"
};

// add TO list
mailMessage.To.Add(new MailAddress("user1@gmail.com", "user1-display-name-is-optional"));
mailMessage.To.Add(new MailAddress("user2@gmail.com", "user2-display-name-is-optional"));
// add CC list
mailMessage.CC.Add(new MailAddress("user3@gmail.com", "user3-display-name-is-optional"));
mailMessage.CC.Add(new MailAddress("user4@gmail.com", "user4-display-name-is-optional"));
// add Bcc list
mailMessage.Bcc.Add(new MailAddress("user5@gmail.com", "user5-display-name-is-optional"));
mailMessage.Bcc.Add(new MailAddress("user6@gmail.com", "user6-display-name-is-optional"));

Create SMTP Client:

var gmailClient = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
Credentials = new NetworkCredential("user1@gmail.com", "password"), // user1 is same as From mail
EnableSsl = true
};

To Send HTML Content:


var htmlText = "<h3>This is html text</h3> <br />Hello<br />" +
"<br /><img src=\"cid:myImageId\"/>";

var htmlContent = AlternateView.CreateAlternateViewFromString(htmlText, null, MediaTypeNames.Text.Html);

// TO linking our image as an embedded resource
var myPicture = new LinkedResource("G:\\Temp\\Capture.JPG", MediaTypeNames.Image.Jpeg)
{
ContentId = "myImageId"
};

htmlContent.LinkedResources.Add(myPicture); // Linking the image
mailMessage.AlternateViews.Add(htmlContent);

To Send Plain Text:

var normalText = AlternateView.CreateAlternateViewFromString("This is normail Text...", null, MediaTypeNames.Text.Plain);
mailMessage.AlternateViews.Add(normalText);

To Send Attachments:

 mailMessage.Attachments.Add(new Attachment(@"G:\Temp\readme.txt"));
Note: You have other options of adding streams to attachments.

Send Email:

gmailClient.Send(mailMessage);



Hope this helps.