Feb 4, 2011
If you are looking for a free email server to use in your C# application, Gmail SMTP server can be a good choice. Using.Net framework built in classes in the System.Net.Mail namespace, you can easily create and send emails via Gmail SMTP server.

Following example is a very simple implementation of using Gmail to send an email.

using System.Net.Mail;

namespace GmailSample.Utility
{
    public class GmailSmtpSample
    {
        public void SendEmail(string to, string subject, string message)
        {
            MailMessage mailMessage = new MailMessage();

            // Set from, to email addresses. Cc and Bcc can be configured in the same way.
            mailMessage.From = new MailAddress("Your Gmail Address"); 
            mailMessage.To.Add(new MailAddress(to));

            // Set email subject and body
            mailMessage.Subject = subject;
            mailMessage.Body = message;

            // You can also add attachments
            mailMessage.Attachments.Add(new Attachment(@"C:\Users\Kalana\Desktop\SampleAttachment.txt"));

            // Define whether email body contain HTML content
            mailMessage.IsBodyHtml = true;

            // Create the SMTP client
            SmtpClient gmailSmtpClient = new SmtpClient();
            gmailSmtpClient.Host = "smtp.gmail.com";
            gmailSmtpClient.Port = 587;
            gmailSmtpClient.EnableSsl = true;

            // Specify authentication details for your Google account
            gmailSmtpClient.Credentials = new System.Net.NetworkCredential("Your Google Username","Your Password"); 

            gmailSmtpClient.Send(mailMessage);
        }

    }
}
That's easy ! Isn't it?

Is Gmail SMTP the right choice?

Using Gmail to send emails for your application can be helpful if you’re working on a small personal project and you just need to get that output email anyway. Also, if you are testing your application and need an SMTP server to test the email functionality, Gmail would be fine.

But before configuring Gmail you have to keep in mind that there are restrictions introduced by Google which limits the number of emails sent per day and the number of recipients per message. So you would only be able to send 2000 mails in a 24-hour period. Anything more will even lead to temporarily blocking your Gmail account.

Therefore, you might want to think about your requirement and the amount of emails you want to send before selecting Gmail as yout SMTP server of choice.

About the Author

Kalana Wijethunga is a Software Developer with experience in designing and developing enterprise applications using .Net technologies. He possesses experience in .Net, C#, ASP MVC, Web API, AngularJS, WPF, SQL Server and Oracle.

0 comments