Send Email from your GMAIL account using ASP.Net and C#

Send email using Gmail account

Download Sample

You can use your gmail account to send emails from ASP.Net and C#, using SMTP. You need to set SSL credentials. This is not straight forward like a simple SMTP email. After searching for long hours I was able to find a solution. Use following class to send emails using your GMAIL account.

Please supply your gmail user name and password in following class where needed.

using System.Web.Mail;
using System;
public class MailSender
 public static bool SendEmail(
 string pGmailEmail, 
 string pGmailPassword, 
 string pTo, 
 string pSubject,
 string pBody, 
 System.Web.Mail.MailFormat pFormat,
 string pAttachmentPath)
 System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
 //sendusing: cdoSendUsingPort, value 2, for sending the message using 
 //the network.

 //smtpauthenticate: Specifies the mechanism used when authenticating 
 //to an SMTP 
 //service over the network. Possible values are:
 //- cdoAnonymous, value 0. Do not authenticate.
 //- cdoBasic, value 1. Use basic clear-text authentication. 
 //When using this option you have to provide the user name and password 
 //through the sendusername and sendpassword fields.
 //- cdoNTLM, value 2. The current process security context is used to 
 // authenticate with the service.
 //Use 0 for anonymous
 myMail.From = pGmailEmail;
 myMail.To = pTo;
 myMail.Subject = pSubject;
 myMail.BodyFormat = pFormat;
 myMail.Body = pBody;
 if (pAttachmentPath.Trim() != "")
 MailAttachment MyAttachment = 
 new MailAttachment(pAttachmentPath);
 myMail.Priority = System.Web.Mail.MailPriority.High;

 System.Web.Mail.SmtpMail.SmtpServer = "";
 return true;
 catch (Exception ex)

Example usage of this class,

                             "to_email @", 
                             "This is email subject" , 
                             "This is email body", 
                             "Physical path to your Attachment")

Note: If you do not want to attach a file with email, supply blank string as "".


Previous Questions Log

Hi Shabdar,

I am new to ASP.Net developement and I want to use my gmail account when sending password retrieval emails. I think the standard controls that visual web developer 2008 provide are really neat but I cant get the app to send the mail via gmail's SMTP.

How should i use your code in such a way that will allow me to send the mails that the controls generate via gmails smtp?
=> Gavin Lumsden (Sunday 20-Apr-08 08:38 AM)
Hi Shabdar,

I want to send email using my gmail account from my laptop. It is connected to the internet, but there is no SMTP service running on it. My question is:
Do i need SMTP service to be running on my laptop to be able to use your code to send email (via gmail account) ?
=> vivek (Tuesday 06-May-08 09:05 AM)
No, you don't need SMTP service running on your laptop/computer/server.
=> Shabdar (Tuesday 06-May-08 12:18 PM)
this was a fantastic article, it worked for me on the first try, however with VS 2008 it tells me some of the items are obsolete (works just find nonetheless)
=> jeff siegel (Saturday 10-May-08 12:39 AM)
You'have just solved me a problem of 2 days - thank you! where did you find this solution??
=> inna (Monday 19-May-08 12:33 PM)
Nice coding....
=> sammy (Thursday 07-Aug-08 05:04 AM)
Marvelous code
i got what i was looking for, thanks you are a gem
=> Gurpreet Singh (Friday 22-Aug-08 07:09 AM)
Hey,, Its a Gr8 code 4 me to handle email sending..

can u gv me idea how to send email from other than gmail.
or any email domain i can use to send email instead of inly using gmail.

Thanks in advance.
=> Praful (Friday 05-Sep-08 01:01 AM)
Hi i am tring to use this code in my hosting server.And it gives me "Mail sent successfully." but i am not recived the mail. Can you please help me out onthis.
=> LiveMyResume (Tuesday 09-Sep-08 03:21 AM)
I downloaded your code project but when i click the send button i get a message "The transport failed to connect to the server" . plz suggest
=> surajit (Tuesday 16-Sep-08 02:43 AM)
when i trued ur code it is shiwing this error

"The transport failed to connect to the server"

kindly give me a solution for this problem


=> Revathy (Wednesday 17-Sep-08 05:08 AM)
go to run>command prompt

and type : telnet 465
if it fails then ur service provider is prohibiting the
use of of might be the case i m not all that sure
but it might be the case.
=> surajit (Thursday 18-Sep-08 02:23 AM)
when i tried ur code it is showing this error

"The transport failed to connect to the server"

kindly give me a solution for this problem


=> Revathy (Wednesday 17-Sep-08 05:11 AM)
please check ur internet connection.
=> abhi (Monday 01-Dec-08 08:46 AM)
Good article to send mails using gmailaccount.
goodjob . .
=> sudhir malireddy (Thursday 25-Sep-08 12:57 AM)
This application is really good. Its works fine.
=> Chandrajit Samanta (Friday 24-Oct-08 03:59 AM)
I made on a framework 2.0 and it works perfectly... but i have an application in framework 1.0 unfortunally doesnt work... any idea?
=> Paulo Pil√£o (Wednesday 12-Nov-08 07:57 PM)
we r creating a fresh project in with c#..,In that Project we have a one module for sending mail options.. help me
=> Ashik (Tuesday 18-Nov-08 05:10 AM)
Hi Shabdar,
It's really nice explanation given by you.... Thanks a lot. ..It really works well. Hope to see such excellent and simple code again....
You have really made our work very easy....It am reffering this site to my friends...
=> Ajay Kewale (Sunday 23-Nov-08 06:58 PM)
how to use yahoo mail server for send email?
=> abhi (Monday 01-Dec-08 08:45 AM)

Posted: 1 month 1 week ago by prabhakar #582
prabhakar's Avatar
very very thanks sir,
iam serching 100 above sites but not working codes
your code was very simple ans perfect working...

once again thank u sir
Posted: 3 years 3 months ago by martinabinaford #259
martinabinaford's Avatar
hi tom i shouldnt give it out but i think this is the contact
and details, they have 20% discount now, mention marts recommened you
Posted: 3 years 9 months ago by dellmerca #198
dellmerca's Avatar
Here you can find sending email from from C# and

Posted: 4 years 3 months ago by tjaank #112
tjaank's Avatar
you can contact me here:
Posted: 4 years 3 months ago by Awon #110
Awon's Avatar
Hello There

I come to know you about Sending Email using

I have a url which can help you.

Click Here :

I hope it is enough for you.

Thank You

Posted: 4 years 4 months ago by michaelD #108
michaelD's Avatar

This exactly what I am looking it possible to speak directly with you?

Posted: 4 years 4 months ago by tjaank #106
tjaank's Avatar
check these for more understanding: