Click here to Skip to main content
13,293,474 members (68,477 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


112 bookmarked
Posted 21 Feb 2011

Using Gmail Account to Send Emails With Attachment

, 26 Sep 2013
Rate this:
Please Sign up or sign in to vote.
Sending Email using Gmail account from your application.

The following piece of code will let you send email using the Google server. Just replace the network credentials with your Google user name and password.

//Short Method
    SmtpClient smtp = new SmtpClient("", 587);
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential("username", "password");
    smtp.EnableSsl = true;
    smtp.Send("", "receiver", "subject", "Email Body");
catch(Exception ex)
//Detailed Method
    MailAddress mailfrom = new MailAddress("");
    MailAddress mailto = new MailAddress("");
    MailMessage newmsg = new MailMessage(mailfrom, mailto);

    newmsg.Subject = "Subject of Email";
    newmsg.Body = "Body(message) of email";

    //For File Attachment, more file can also be attached
    Attachment att = new Attachment("C:\\...file path");

    SmtpClient smtp = new SmtpClient("", 587);
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential("username","password");
    smtp.EnableSsl = true;
catch(Exception ex)

Note: Before providing the credentials, you need to set UserDefaultCredentials to false. The above mentioned settings are for Gmail, but you can also use other SMTP servers like Yahoo!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pakistan Pakistan
I am a learner.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
mkdir22-May-14 19:40
membermkdir22-May-14 19:40 
GeneralMy vote of 5 Pin
Gun Gun Febrianza27-Nov-13 9:46
member Gun Gun Febrianza27-Nov-13 9:46 
GeneralMy vote of 1 Pin
VMAtm30-Sep-13 4:21
memberVMAtm30-Sep-13 4:21 
GeneralMy vote of 5 Pin
Sk. Tajbir27-Sep-13 0:17
professionalSk. Tajbir27-Sep-13 0:17 
Questionwhy i have this error?? Pin
ma.amer4-Apr-12 1:58
memberma.amer4-Apr-12 1:58 
GeneralReason for my vote of 2 u missed namespase, using System.Net... Pin
harath randika14-Jan-12 6:06
memberharath randika14-Jan-12 6:06 
GeneralReason for my vote of 2 So Simple Pin
Assil13-Jan-12 14:08
memberAssil13-Jan-12 14:08 
Reason for my vote of 2
So Simple
GeneralRe: Reason for my vote of 2So Simple Pin
adriancs26-Sep-13 17:08
mvpadriancs26-Sep-13 17:08 
GeneralReason for my vote of 5 nice Pin
eduveliz261215-Dec-11 7:08
membereduveliz261215-Dec-11 7:08 
GeneralThanks, I know this will be useful to me at some point. I r... Pin 19:27 19:27 
Generalnice one Pin
Shahriar Iqbal Chowdhury2-May-11 11:16
memberShahriar Iqbal Chowdhury2-May-11 11:16 
Generalsimple and working, nice article... BTW, if we can work wit... Pin
madper26-Apr-11 8:59
membermadper26-Apr-11 8:59 
GeneralI just tried it and it works great! I've been trying to use ... Pin
Keith.Badeau9-Apr-11 19:53
memberKeith.Badeau9-Apr-11 19:53 
GeneralReason for my vote of 5 Had not realized until reading this ... Pin
cws2_na4-Apr-11 3:42
membercws2_na4-Apr-11 3:42 
GeneralI haven't tried it yet but I plan to test it out as soon as ... Pin
Keith.Badeau1-Apr-11 9:54
memberKeith.Badeau1-Apr-11 9:54 
GeneralReason for my vote of 5 Excellent article, which IMHO should... Pin
DrABELL30-Mar-11 7:15
memberDrABELL30-Mar-11 7:15 
GeneralReason for my vote of 5 Very simple and cool! Pin
Tony's Toy29-Mar-11 19:20
memberTony's Toy29-Mar-11 19:20 
GeneralReason for my vote of 4 Needed some using statements to make... Pin
icodenmidwest29-Mar-11 6:29
membericodenmidwest29-Mar-11 6:29 
GeneralReason for my vote of 1 :( Pin
Member 167658929-Mar-11 1:46
memberMember 167658929-Mar-11 1:46 
GeneralRe: Reason for my vote of 1:( Pin
adriancs26-Sep-13 17:15
mvpadriancs26-Sep-13 17:15 
GeneralReason for my vote of 4 I could be generic. 4/5 Pin
Abdul Quader Mamun28-Mar-11 17:39
memberAbdul Quader Mamun28-Mar-11 17:39 
GeneralReason for my vote of 4 nc snippet Pin
C.Sniper28-Mar-11 0:09
memberC.Sniper28-Mar-11 0:09 
GeneralReason for my vote of 5 thanx Pin
rahulkinikar22-Mar-11 4:07
memberrahulkinikar22-Mar-11 4:07 
GeneralComment from Shahil shaikh351: hi...... Abdur Rehman Raza K... Pin
Sandeep Mewara22-Mar-11 3:25
mvpSandeep Mewara22-Mar-11 3:25 
GeneralRe: i think u r missing <code> smtp.EnableSsl = true; </code> Pin
Abdur Rehman Raza Khan - dontumindit5-Apr-11 2:30
memberAbdur Rehman Raza Khan - dontumindit5-Apr-11 2:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 26 Sep 2013
Article Copyright 2011 by dontumindit
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid