Shabdar.org
Webshabdar.org
Send Email from your GMAIL account using ASP.Net and C# PDF Print E-mail
Written by Shabdar   
Monday, 24 November 2008 12:00

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)
    {
    try
    {
        System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
        myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
                          "smtp.gmail.com");
        myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
                          "465");
        myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendusing",
                          "2");
        //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.
        myMail.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");
        //Use 0 for anonymous
        myMail.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/sendusername",
            pGmailEmail);
        myMail.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
             pGmailPassword);
        myMail.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
             "true");
        myMail.From = pGmailEmail;
        myMail.To = pTo;
        myMail.Subject = pSubject;
        myMail.BodyFormat = pFormat;
        myMail.Body = pBody;
        if (pAttachmentPath.Trim() != "")
        {
            MailAttachment MyAttachment = 
                    new MailAttachment(pAttachmentPath);
            myMail.Attachments.Add(MyAttachment);
            myMail.Priority = System.Web.Mail.MailPriority.High;
        }

        System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
        System.Web.Mail.SmtpMail.Send(myMail);
        return true;
    }
    catch (Exception ex)
    {
        throw;
    }
}
}


Example usage of this class,

EmailLib.SendEmail.SendEmail("your_gmail_id", 
                             "your_gmail_password", 
                             "to_email@anydomain.com", 
                             "This is email subject" , 
                             "This is email body", 
                             Web.Mail.MailFormat.Text, 
                             "Physical path to your Attachment")


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

Comments/Questions

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)
Reply
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)
Reply
No, you don't need SMTP service running on your laptop/computer/server.
=> Shabdar (Tuesday 06-May-08 12:18 PM)
Reply
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)
Reply
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)
Reply
Nice coding....
=> sammy (Thursday 07-Aug-08 05:04 AM)
Reply
Marvelous code
i got what i was looking for, thanks you are a gem
=> Gurpreet Singh (Friday 22-Aug-08 07:09 AM)
Reply
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)
Reply
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.
ManiVannan.M
=> LiveMyResume (Tuesday 09-Sep-08 03:21 AM)
Reply
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)
Reply
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

regards

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

and type : telnet smtp.gmail.com 465
if it fails then ur service provider is prohibiting the
use of of gmail.it might be the case i m not all that sure
but it might be the case.
=> surajit (Thursday 18-Sep-08 02:23 AM)
Reply
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

regards

revathy
=> Revathy (Wednesday 17-Sep-08 05:11 AM)
Reply
please check ur internet connection.
=> abhi (Monday 01-Dec-08 08:46 AM)
Reply
Good article to send mails using gmailaccount.
goodjob . .
u
Thanks
Sudhir.M
=> sudhir malireddy (Thursday 25-Sep-08 12:57 AM)
Reply
This application is really good. Its works fine.
=> Chandrajit Samanta (Friday 24-Oct-08 03:59 AM)
Reply
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)
Reply
hi.,
we r creating a fresh project in asp.net with c#..,In that Project we have a one module for sending mail options.. help me
=> Ashik (Tuesday 18-Nov-08 05:10 AM)
Reply
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)
Reply
how to use yahoo mail server for send email?
=> abhi (Monday 01-Dec-08 08:45 AM)
Reply


{mos_fb_discuss:11}

Comments
Add New Search
+/-
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
Please input the anti-spam code that you can read in the image.
Eraslan Peker  - Thank you |81.215.15.xxx |2009-03-25 01:36:19
i am very glad , thank you so much this is realy good.
Hameed  - nothing |195.229.235.xxx |2009-03-30 10:33:05
Hi Shabdar,

Excellent work...keep it up and please email me good articles for code like this
one....
Vasu |195.153.160.xxx |2009-04-07 01:55:08
:( Hi,

I have downloaded ur code and run it and it showing that traction to the server
is failed!
anu  - getting error... help me... |116.75.88.xxx |2009-04-08 21:55:56
hi
on the above code i m getting error..


help me......
Ray Loveless  - Time out error. |67.137.23.xxx |2009-04-17 09:25:28
Mine won't connect to the server for some reason. my C# app is running XP.
would my service provider be blocking this for some reason...? any suggestion
thanks.
saurabh singh  - Getting Error |117.198.2.xxx |2009-04-26 22:59:12
The message could not be sent to the SMTP server. The transport error code was
0x80040217. The server response was not available
Haisum  - 0x80040217 |59.103.194.xxx |2010-02-01 14:10:08
Hey I go the same. And believe me it's one of silliest mistakes I did....Are u
giving your GMAIL id in : "xyz@gmail.com"? don't do that just put
"xyz" in email id box....program adds the rest. It should work then
Anonymous |82.114.160.xxx |2009-05-06 22:42:12
i want code in the c#

in the mail
Tomi  - AWESOME WORK |125.164.73.xxx |2009-05-25 00:43:14
Hi, u really2 solve my problem..NICE ARTICLE
4 thumbs up...
Libero  - Thenks |93.42.235.xxx |2009-11-25 11:15:13
This was very very very useful.
Thanks a lot!
Ajay Borkar  - Help Me !!! |117.198.80.xxx |2009-11-25 15:09:21
when i try your code that get me this error



-->>" The requested body part was not found in this message. "



anuj |115.240.119.xxx |2009-12-05 15:04:30
hi!
i'm trying to use this code in my application but when i hit the submit button i
see this error-

"The message could not be sent to the SMTP server. The transport error code
was 0x80040217. The server response was not available".

please help me for this.
deepu |122.173.134.xxx |2010-01-15 02:12:53
hi!
i'm trying to use this code in my application but when i hit the submit button i
see this error-

"The message could not be sent to the SMTP server. The transport error code
was 0x80040217. The server response was not available".

please help me for this.
Haisum  - Solution |59.103.194.xxx |2010-02-01 14:10:53
Hey I go the same. And believe me it's one of silliest mistakes I did....Are u
giving your GMAIL id in : "xyz@gmail.com"? don't do that just put
"xyz" in email id box....program adds the rest. It should work then
Haisum  - Getting Error |59.103.194.xxx |2010-02-01 13:55:09
0x80040217 getting error
Yash  - Hello |60.243.67.xxx |2010-02-14 05:40:41
Server Error in '/' Application.

Runtime Error

Description: An application error occurred on the server. The current custom
error settings for this application prevent the details of the application error
from being viewed remotely (for security reasons). It could, however, be viewed
by browsers running on the local server machine.

Details: To enable the details of this specific error message to be viewable on
remote machines, please create a tag within a "web.config"
configuration file located in the root directory of the current web application.
This tag should then have its "mode" attribute set to "Off".










Notes: The current error page you are seeing can be replaced by a custom error
page by modifying the "defaultRedirect" attribute of the application's
configuration tag to point to a custom error page URL.









Yash |60.243.67.xxx |2010-02-14 05:41:57
I got this error , how to solve it, i just want to use this script for form to
email. Please help me !

priyanka |121.245.4.xxx |2010-02-23 02:39:43
hi i m getting error
The server rejected one or more recipient addresses. The server response was:
553 5.1.2 or other punctuation after the recipient's email address.
4sm1347070ywg.54
plz help me out
priyanka  - re: |121.245.4.xxx |2010-02-23 03:10:59
its really nice!!!!!!!!!!! .thanks a lot
sejal  - output not shown |220.224.237.xxx |2010-02-27 07:32:11
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.

sejal doshi
Mazharkhan  - Mazhar |122.169.95.xxx |2010-03-03 08:50:58
Hi Dear,



Thanks a lot. this code help me alot.



again thanks,

Mazhar
Anonymous |94.182.163.xxx |2010-08-13 07:29:10
The message could not be sent to the SMTP server. The transport error code was
0x80040217. The server response was not available
Satish  - re: Solution |1.23.81.xxx |2010-08-17 14:26:15
tHANKS DUDE
Last Updated on Tuesday, 23 March 2010 11:19