Click here to Skip to main content
Click here to Skip to main content

Tagged as

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
try
{
    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential("username", "password");
    smtp.EnableSsl = true;
    smtp.Send("sender@gamil.com", "receiver", "subject", "Email Body");
}
catch(Exception ex)
{
    Console.WriteLine(ex.Message);
}
//Detailed Method
try
{
    MailAddress mailfrom = new MailAddress("sender@gmail.com");
    MailAddress mailto = new MailAddress("receiver@abc.com");
    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");
    newmsg.Attachments.Add(att);

    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential("username","password");
    smtp.EnableSsl = true;
    smtp.Send(newmsg);    
} 
catch(Exception ex)
{
    Console.WriteLine(ex.Message);
}

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!

License

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

Share

About the Author

dot net tech

Pakistan Pakistan
I am programming learner.

Comments and Discussions

 
GeneralMy vote of 3 Pinmembermkdir22-May-14 18:40 
GeneralMy vote of 5 Pinmember Gun Gun Febrianza27-Nov-13 8:46 
GeneralMy vote of 1 PinmemberVMAtm30-Sep-13 3:21 
GeneralMy vote of 5 PinprofessionalSk. Tajbir26-Sep-13 23:17 
Questionwhy i have this error?? Pinmemberma.amer4-Apr-12 0:58 
GeneralReason for my vote of 2 u missed namespase, using System.Net... Pinmemberharath randika14-Jan-12 5:06 
GeneralReason for my vote of 2 So Simple PinmemberAssil13-Jan-12 13:08 
GeneralRe: Reason for my vote of 2So Simple Pinmvpadriancs26-Sep-13 16:08 
GeneralReason for my vote of 5 nice Pinmembereduveliz261215-Dec-11 6:08 
GeneralThanks, I know this will be useful to me at some point. I r... Pinmemberathenatennis.com.sg9-May-11 18:27 
Generalnice one PinmemberShahriar Iqbal Chowdhury2-May-11 10:16 
Generalsimple and working, nice article... BTW, if we can work wit... Pinmembermadper26-Apr-11 7:59 
GeneralI just tried it and it works great! I've been trying to use ... PinmemberKeith.Badeau9-Apr-11 18:53 
GeneralReason for my vote of 5 Had not realized until reading this ... Pinmembercws2_na4-Apr-11 2:42 
GeneralI haven't tried it yet but I plan to test it out as soon as ... PinmemberKeith.Badeau1-Apr-11 8:54 
GeneralReason for my vote of 5 Excellent article, which IMHO should... PinmemberDrABELL30-Mar-11 6:15 
GeneralReason for my vote of 5 Very simple and cool! PinmemberTony's Toy29-Mar-11 18:20 
GeneralReason for my vote of 4 Needed some using statements to make... Pinmembericodenmidwest29-Mar-11 5:29 
GeneralReason for my vote of 1 :( PinmemberMember 167658929-Mar-11 0:46 
GeneralRe: Reason for my vote of 1:( Pinmvpadriancs26-Sep-13 16:15 
GeneralReason for my vote of 4 I could be generic. 4/5 PinmemberAbdul Quader Mamun28-Mar-11 16:39 
GeneralReason for my vote of 4 nc snippet PinmemberC.Sniper27-Mar-11 23:09 
GeneralReason for my vote of 5 thanx Pinmemberrahulkinikar22-Mar-11 3:07 
GeneralComment from Shahil shaikh351: hi...... Abdur Rehman Raza K... PinmvpSandeep Mewara22-Mar-11 2:25 
GeneralRe: i think u r missing <code> smtp.EnableSsl = true; </code> PinmemberAbdur Rehman Raza Khan - dontumindit5-Apr-11 1:30 
GeneralReason for my vote of 5 Simple and Useful.... Great work bro... PinmemberPravin Patil, Mumbai1-Mar-11 21:31 
GeneralRe: welcome PinmemberAbdur Rehman Raza Khan - dontumindit12-Mar-11 1:20 
GeneralReason for my vote of 4 Thank's bro... Pinmemberashishkumar0081-Mar-11 18:40 
GeneralReason for my vote of 5 Very useful code, especially for qui... Pinmemberjfevia25-Feb-11 15:07 
GeneralRe: hmmmm thnx PinmemberAbdur Rehman Raza Khan - dontumindit26-Feb-11 21:50 
Generalthnxxxxxxxxxxx dear... PinmemberAbdur Rehman Raza Khan - dontumindit22-Feb-11 16:17 
GeneralReason for my vote of 5 Nice and simple. Now I'll need to fi... PinmemberJF201521-Feb-11 20:34 
GeneralReason for my vote of 4 Seems nice! PinmemberGPUToaster21-Feb-11 18:18 
GeneralBecause... Pinmemberdamnedyankee24-Feb-11 5:06 
GeneralRe: Because... PinmemberAbdur Rehman Raza Khan - dontumindit26-Feb-11 5:45 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 26 Sep 2013
Article Copyright 2011 by dot net tech
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid