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

Using Gmail Account to Send Emails With Attachment

By , 20 May 2011
 
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.
private void button_send_Click(object sender, EventArgs e)
{
    //Short Method
            
    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");
 
    //Detailed Method
            
    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);    
}
 

I have kept the code simple, using or try-catch blocks can be added as per need.
Have a nice time. Do give suggestions and comments.
 
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.

License

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

About the Author

dot net tech
Pakistan Pakistan
Member
I am programming learner.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionwhy i have this error??memberma.amer4 Apr '12 - 0:58 
Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
GeneralReason for my vote of 2 u missed namespase, using System.Net...memberharath randika14 Jan '12 - 5:06 
Reason for my vote of 2
u missed namespase,
using System.Net.Mail;
GeneralReason for my vote of 2 So SimplememberAssil13 Jan '12 - 13:08 
Reason for my vote of 2
So Simple
GeneralReason for my vote of 5 nicemembereduveliz261215 Dec '11 - 6:08 
Reason for my vote of 5
nice
GeneralThanks, I know this will be useful to me at some point. I r...memberathenatennis.com.sg9 May '11 - 18:27 
Thanks, I know this will be useful to me at some point. I remember trying to do this a while back in Excel VBA using the outlook component.
 
--
www.athenatennis.com.sg
Nurturing creativity and talent in kids.
Generalnice onememberShahriar Iqbal Chowdhury2 May '11 - 10:16 
nice one
Generalsimple and working, nice article... BTW, if we can work wit...membermadper26 Apr '11 - 7:59 
simple and working, nice article...
 
BTW, if we can work with SMTP in this simple way, anyone know how to work with POP3 just as simple as this?
GeneralI just tried it and it works great! I've been trying to use ...memberKeith.Badeau9 Apr '11 - 18:53 
I just tried it and it works great! I've been trying to use this API on my own server but I cannot configure the SMTP server for the life of me. This is a nice, easy shortcut. Thanks.
GeneralReason for my vote of 5 Had not realized until reading this ...membercws2_na4 Apr '11 - 2:42 
Reason for my vote of 5
Had not realized until reading this article that port 587 could be used!
Excellent article, short and to the point.
GeneralI haven't tried it yet but I plan to test it out as soon as ...memberKeith.Badeau1 Apr '11 - 8:54 
I haven't tried it yet but I plan to test it out as soon as I have time. Thanks it looks like something I'm going to enjoy playing around with...
GeneralReason for my vote of 5 Excellent article, which IMHO should...memberDrABELL30 Mar '11 - 6:15 
Reason for my vote of 5
Excellent article, which IMHO should be combined with Alternate 3, describing the VB implementation, in order to provide flexible, universal .NET solution pertinent to the topic. 5*
GeneralReason for my vote of 5 Very simple and cool!memberTony's Toy29 Mar '11 - 18:20 
Reason for my vote of 5
Very simple and cool!
GeneralReason for my vote of 4 Needed some using statements to make...membericodenmidwest29 Mar '11 - 5:29 
Reason for my vote of 4
Needed some using statements to make it work.
GeneralReason for my vote of 1 :(memberMember 167658929 Mar '11 - 0:46 
Reason for my vote of 1
:(
GeneralReason for my vote of 4 I could be generic. 4/5memberAbdul Quader Mamun28 Mar '11 - 16:39 
Reason for my vote of 4
I could be generic. 4/5
GeneralReason for my vote of 4 nc snippetmemberC.Sniper27 Mar '11 - 23:09 
Reason for my vote of 4
nc snippet
GeneralReason for my vote of 5 thanxmemberrahulkinikar22 Mar '11 - 3:07 
Reason for my vote of 5
thanx
GeneralComment from Shahil shaikh351: hi...... Abdur Rehman Raza K...mvpSandeep Mewara22 Mar '11 - 2:25 
Comment from Shahil shaikh351:
hi...... Abdur Rehman Raza Khan
one Error Is showing me pls can u solve my this problem
error: System.Net.Mail.SmtpException: The SMTP server requires a
secure connection or the client was not authenticated. The server
response was: 5.7.0 No AUTH command has been given.
 
showing this error bro can u help me to solve this problem
GeneralRe: i think u r missing <code> smtp.EnableSsl = true; </code> memberAbdur Rehman Raza Khan - dontumindit5 Apr '11 - 1:30 
i think u r missing

smtp.EnableSsl = true;

 

GeneralReason for my vote of 5 Simple and Useful.... Great work bro...memberPravin Patil, Mumbai1 Mar '11 - 21:31 
Reason for my vote of 5
Simple and Useful....
Great work bro..
GeneralRe: welcomememberAbdur Rehman Raza Khan - dontumindit12 Mar '11 - 1:20 
welcome
GeneralReason for my vote of 4 Thank's bro...memberashishkumar0081 Mar '11 - 18:40 
Reason for my vote of 4
Thank's bro...
GeneralReason for my vote of 5 Very useful code, especially for qui...memberjfevia25 Feb '11 - 15:07 
Reason for my vote of 5
Very useful code, especially for quick responses.
GeneralRe: hmmmm thnxmemberAbdur Rehman Raza Khan - dontumindit26 Feb '11 - 21:50 
hmmmm thnx
Generalthnxxxxxxxxxxx dear...memberAbdur Rehman Raza Khan - dontumindit22 Feb '11 - 16:17 
thnxxxxxxxxxxx dear...
GeneralReason for my vote of 5 Nice and simple. Now I'll need to fi...memberJF201521 Feb '11 - 20:34 
Reason for my vote of 5
Nice and simple. Now I'll need to find some use for it. Thanks for sharing.
GeneralReason for my vote of 4 Seems nice!memberGPUToaster21 Feb '11 - 18:18 
Reason for my vote of 4
Seems nice!
GeneralBecause...memberdamnedyankee24 Feb '11 - 5:06 
...there aren't enough articles showing how to use the .NET smtp api already.
 
Keep up the good work. May I suggest some other topics for you.
 
1.) Use ODBC to query a database
2.) Use Sockets to stream TCP
3.) Use threads to do background tasks
.....
GeneralRe: Because...memberAbdur Rehman Raza Khan - dontumindit26 Feb '11 - 5:45 
hmmmmmm thnx for suggesting, wil follow it

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 20 May 2011
Article Copyright 2011 by dot net tech
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid