Click here to Skip to main content
15,120,620 members
Articles / Desktop Programming / System
Tip/Trick
Posted 21 Feb 2011

Tagged as

Stats

132.4K views
111 bookmarked

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
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)

Share

About the Author

dontumindit
Pakistan Pakistan
I am a learner.

Comments and Discussions

 
GeneralReason for my vote of 5 Simple and Useful.... Great work bro... Pin
Pravin Patil, Mumbai1-Mar-11 22:31
MemberPravin Patil, Mumbai1-Mar-11 22:31 
GeneralRe: welcome Pin
dontumindit12-Mar-11 2:20
Memberdontumindit12-Mar-11 2:20 
GeneralReason for my vote of 4 Thank's bro... Pin
ashishkumar0081-Mar-11 19:40
Memberashishkumar0081-Mar-11 19:40 
GeneralReason for my vote of 5 Very useful code, especially for qui... Pin
jfevia25-Feb-11 16:07
Memberjfevia25-Feb-11 16:07 
GeneralRe: hmmmm thnx Pin
dontumindit26-Feb-11 22:50
Memberdontumindit26-Feb-11 22:50 
Generalthnxxxxxxxxxxx dear... Pin
dontumindit22-Feb-11 17:17
Memberdontumindit22-Feb-11 17:17 
GeneralReason for my vote of 5 Nice and simple. Now I'll need to fi... Pin
JF201521-Feb-11 21:34
MemberJF201521-Feb-11 21:34 
GeneralReason for my vote of 4 Seems nice! Pin
GPUToaster™21-Feb-11 19:18
MemberGPUToaster™21-Feb-11 19:18 
Reason for my vote of 4
Seems nice!
GeneralBecause... Pin
damnedyankee24-Feb-11 6:06
Memberdamnedyankee24-Feb-11 6:06 
GeneralRe: Because... Pin
dontumindit26-Feb-11 6:45
Memberdontumindit26-Feb-11 6: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.