Thursday, July 26, 2007

Sending E-Mail from your .NET application using your GMail Account


Most independent developers like to include a msg sending feature in their application.
Well atleast I tried to.. Of course it is not a problem for a user with a smtp server
access or a registered domain with an smtp server access. But unfortunately I had
none of them. I use msn, gmail and yahoo for my mailing needs. Unfortunately yahoo
does not offer smtp or pop access for their general/basic/free users. And msn it
is not that reliable coz I was expecting a lot of mails to be sent and also to recive
and as far as I know they also donot give you pop/smtp access!.

Typical Example you will find in internet

So I really Hunted for a code segment that help me use my free gmail account to
send E-MAIL from my .NET account.
is a complete referance library and FAQ's regarding to the System.Web.Mail
namespase. And my code is basically I joined different pieces together. And Came
up with succes. Normally you will find thousands of sites offering the code below
which has a very little use. Like

MailMessage mail = new MailMessage();
mail.To = "";
mail.Bcc = ";";
mail.From = "your email or name";
mail.Subject = "your subject";
mail.Body = "your subject";
SmtpMail.SmtpServer = ""; //your
real server goes here or

//you can add the address like ""

Well most smtp server require authentication and sometimes SSL connection so what
then? The code above is USELESS. So what are the things you need
to add? There you go.

//This is fully working tested code. Copy and paste the code
//Edit some parts(UserName, password) and ENJOY!
public string sendMail (string from, string to, string cc, string bcc, string subject, string body) {
// Mail initialization
MailMessage mailMsg = new MailMessage();
mailMsg.From = from;
mailMsg.To = to;
mailMsg.Cc = cc;
mailMsg.Bcc = bcc;
mailMsg.Subject = subject;
mailMsg.BodyFormat = MailFormat.Text;
mailMsg.Body = body;
mailMsg.Priority = MailPriority.High;
// Smtp configuration
SmtpMail.SmtpServer = "";
// - use smtp authentication
mailMsg.Fields.Add("", "1");
mailMsg.Fields.Add("", "");
mailMsg.Fields.Add("", "mypassword");
// - use port 465 or 587
mailMsg.Fields.Add("", "465");
// - use STARTTLS (some call this SSL)
mailMsg.Fields.Add("", "true");
// try to send Mail
return "";
catch (Exception ex)
return ex.Message;

How to use

Copy the above methods and paste in visual studio IDE or any text editor you use.
Add a referance to System.Web or System.Web.Mail dll change your account
info like your mail address, password, etc and Enjoy

Explanation to the code

So what is happening on the example? See

step 1: Create a mail msg object and asign the basic parts like from, To,
cc, bcc subject, body.

step 2: Add some config settings in fields collection of the mailMessage
object. Type of authentication we use basic authentication and your user name, password,
smtp servers port number that you will be using. for example:

//basic authentication
mailMsg.Fields.Add("", "1");
//sender email address
mailMsg.Fields.Add("", "");
//plaintext password.
//you can encrypt your password and keep in a config file
when you parse just decrypt it

mailMsg.Fields.Add("", "mypassword");
// port number - use port 465 or 587. use anyone
of them

you can even make some grouping so different groups use different port to share
the load on server

mailMsg.Fields.Add("", "465");

gmail username require full gmail address as username and you can use /connect
to any of the port.

step 3: This is the coolest and most important part that I had a lot of time
to spend to discover.

mailMsg.Fields.Add("", "true");
//Cool. Ha? Donot Change! This part is a must if you want to
access GMAIL's SMTP server

This line says/declares that your smtp server requires SSL connection which is very
much true/necessary for accessing to gmail's SMTP server.

step 4: assign the smtp server name in System.Web.Mail.SmtpMail class's Smtp
server object like:

SmtpMail.SmtpServer = "";

you can also set the priority of your mail by

mailMsg.Priority = MailPriority.High;// MailPriority is an
enum. It has High,Low, Normal option

step 5: call the send method of System.Web.Mail.SmtpMail. e.g.


.NET2.0 Version

Some Changes are required for .NET v2.0 as .NET2.0 Mail is much organised and stable
and so many extrafeatures. So In the Update I am including .NET2.0 version of my

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
//Mime is Not necerrary if you dont change the msgview and

//if you dont add custom/extra headers
using System.Threading;

namespace SendMailUsingGmail
public partial class Form1 : Form
public Form1()

static bool mailSent = false;

public void SendMail()
//Builed The MSG
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new MailAddress("", "One Ghost",System.Text.Encoding.UTF8);
msg.Subject = "Test mail using .net2.0";
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = "This is my msg Body";
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = false;
msg.Priority = MailPriority.High;

//Add the Creddentials
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("", "SecretPass");
client.Port = 587;//or use

client.Host = "";
client.EnableSsl = true;
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
object userState=msg;
//you can also call client.Send(msg)
client.SendAsync(msg, userState);
catch (System.Net.Mail.SmtpException ex)
MessageBox.Show(ex.Message, "Send Mail Error");

void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
MailMessage mail = (MailMessage)e.UserState;
string subject = mail.Subject;

if (e.Cancelled)
string cancelled = string.Format("[{0}] Send canceled.", subject);
if (e.Error != null)
string error = String.Format("[{0}] {1}", subject, e.Error.ToString());
MessageBox.Show("Message sent.");
mailSent = true;

private void button1_Click(object sender, EventArgs e)

Limitation of this code

This code may/will not work with .NET framework v1.0. Because the MailMessage object
of .NETv1.0 does not have the MailMessage.Fields Collection. This was added
in .NETv1.1. So it works fine with .NETv1.1 and v2.0.


The send method does not return anything. But you can add some mechanism(like counter)
to keep track of send message or even keep the return type of your method as a mailmessage
type so that u can save to database for security reasons.