Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / System
Tip/Trick

Using Gmail Account to Send Emails With Attachment

Rate me:
Please Sign up or sign in to vote.
4.76/5 (70 votes)
26 Sep 2013CPOL 151.2K   110   36
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.

C#
//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);
}
C#
//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)


Written By
Pakistan Pakistan
I am a learner.

Comments and Discussions

 
GeneralRe: Reason for my vote of 1:( Pin
adriancs26-Sep-13 16:15
mvaadriancs26-Sep-13 16:15 
GeneralReason for my vote of 4 I could be generic. 4/5 Pin
Abdul Quader Mamun28-Mar-11 16:39
Abdul Quader Mamun28-Mar-11 16:39 
GeneralReason for my vote of 4 nc snippet Pin
C.Sniper27-Mar-11 23:09
C.Sniper27-Mar-11 23:09 
GeneralReason for my vote of 5 thanx Pin
rahulkinikar22-Mar-11 3:07
rahulkinikar22-Mar-11 3:07 
GeneralComment from Shahil shaikh351: hi...... Abdur Rehman Raza K... Pin
Sandeep Mewara22-Mar-11 2:25
mveSandeep Mewara22-Mar-11 2:25 
GeneralRe: i think u r missing <code> smtp.EnableSsl = true; </code> Pin
dontumindit5-Apr-11 1:30
dontumindit5-Apr-11 1:30 
GeneralReason for my vote of 5 Simple and Useful.... Great work bro... Pin
Pravin Patil, Mumbai1-Mar-11 21:31
Pravin Patil, Mumbai1-Mar-11 21:31 
GeneralRe: welcome Pin
dontumindit12-Mar-11 1:20
dontumindit12-Mar-11 1:20 
welcome
GeneralReason for my vote of 4 Thank's bro... Pin
ashishkumar0081-Mar-11 18:40
ashishkumar0081-Mar-11 18:40 
GeneralReason for my vote of 5 Very useful code, especially for qui... Pin
jfevia25-Feb-11 15:07
jfevia25-Feb-11 15:07 
GeneralRe: hmmmm thnx Pin
dontumindit26-Feb-11 21:50
dontumindit26-Feb-11 21:50 
Generalthnxxxxxxxxxxx dear... Pin
dontumindit22-Feb-11 16:17
dontumindit22-Feb-11 16:17 
GeneralReason for my vote of 5 Nice and simple. Now I'll need to fi... Pin
JF201521-Feb-11 20:34
JF201521-Feb-11 20:34 
GeneralReason for my vote of 4 Seems nice! Pin
GPUToaster™21-Feb-11 18:18
GPUToaster™21-Feb-11 18:18 
GeneralBecause... Pin
damnedyankee24-Feb-11 5:06
damnedyankee24-Feb-11 5:06 
GeneralRe: Because... Pin
dontumindit26-Feb-11 5:45
dontumindit26-Feb-11 5:45 

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.