Gmail in C#

Use C# to send mail using Google’s Gmail. If you’ve ever wanted to send email using your Gmail account then take a look.


using System.Net;
using System.Net.Mail;

namespace JarlooGmail
    public class Gmail
        public string Username { get; set; }
        public string Password { get; set; }

        public Gmail(string username, string password)
            Username = username;
            Password = password;

        public void Send(MailMessage msg)
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new NetworkCredential(Username, Password);

You can call the Gmail c# class like so:

using System.Net.Mail;

namespace JarlooGmail
    internal class Program
        private static void Main(string[] args)
            Gmail gmail = new Gmail("MyUsername","MyPassword");
            MailMessage msg = new MailMessage("FromMe@here", "ToYou@there");
            msg.Subject = "my subject";
            msg.Body = "the body of the message";

Categories:   Code

Tags:  , ,


  • Posted: May 3, 2012 17:00


    Thanks a lot for this example, I got it to work in 2 minutes. Keep sharing
  • Posted: June 14, 2012 09:13

    Saeed Neamati

    This was truly helpful to me. But I don't know how many emails per day can we send? I mean, does Google ban us as spammers for sending more than a specified number?
    • Posted: June 14, 2012 10:04


      Google does have a limit but it is set fairly high. Once you exceed the limit they will prevent you from sending emails for a period of time. (It was a few hours if I recall.) This happened to me once as I use Gmail to send me errors from my applications and something that runs every 15 seconds had a problem and so the application started spamming me with emails. (yes bad design on my part) What I don't know is if you keep getting these temporary bans will they eventually make it permanent?
  • Posted: October 11, 2012 20:06


    Thanks, just what I was looking for!
  • Posted: June 23, 2013 06:24


  • Posted: August 14, 2013 08:13


    Thanks a lot. It's very helpfull!
  • Posted: August 9, 2015 05:10


    Can't get trhis (or other) examples tp work! Keep getting an exception: An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll. Additional information: Failure sending mail. Any help anyoone???