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);
client.Send(msg);
}
}
}
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";
gmail.Send(msg);
}
}
}
4 Comments
Join the conversation and post a comment.



Thanks a lot for this example,
I got it to work in 2 minutes.
Keep sharing
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?
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?
Thanks, just what I was looking for!