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

Using Gmail Account to Send Emails With Attachment

Rate me:
Please Sign up or sign in to vote.
4.75/5 (6 votes)
5 Apr 2011CPOL 16.3K   4   5
public class MailClass { public static void CreateMailAddresses(string sender, string reciever, out MailAddress mailfrom, out MailAddress mailto) { MailAddress Mailfrom = new MailAddress(sender); MailAddress Mailto = new...
C#
public class MailClass
        {
            public static void CreateMailAddresses(string sender, string reciever, out MailAddress mailfrom, out MailAddress mailto)
            {
                MailAddress Mailfrom = new MailAddress(sender);
                MailAddress Mailto = new MailAddress(reciever);
                mailfrom = Mailfrom;
                mailto = Mailto;
            }

            public static MailMessage MailMessage(MailAddress mailfrom, MailAddress mailto, string subject, string body)
            {
                MailMessage newmsg = new MailMessage(mailfrom, mailto);

                newmsg.Subject = subject;
                newmsg.Body = body;
                newmsg.IsBodyHtml = true;
                return newmsg;
            }

            public static SmtpClient SmtpClient(string sender, string password, string host, bool SslEnabled, int port)
            {
                SmtpClient smtp = new SmtpClient(host, port);
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential(sender, password);
                smtp.EnableSsl = SslEnabled;
                return smtp;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {        

            MailAddress mailfrom;
            MailAddress mailto;
            MailClass.CreateMailAddresses("sender@gmail.com", "reciever@ymail.com", out mailfrom, out mailto);
            MailMessage newmsg = MailClass.MailMessage(mailfrom, mailto, "Subject of Email", "Body(message) of email");
            SmtpClient smtp = MailClass.SmtpClient("sender@gmail.com", "password", "smtp.gmail.com", true, 587);
            smtp.Send(newmsg);
        }

License

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


Written By
United States United States
I do not claim to be wrong! I just rarely ever write.

Comments and Discussions

 
Generalwhat is a new? ok create one address but what about the othe... Pin
charles henington15-May-11 4:04
charles henington15-May-11 4:04 
GeneralReason for my vote of 3 CreateMailAddresses function is not ... Pin
Philippe Mori14-May-11 10:25
Philippe Mori14-May-11 10:25 
GeneralReason for my vote of 5 very nice i will use this Pin
cSNewb22-Apr-11 6:03
cSNewb22-Apr-11 6:03 
GeneralSending mail with attachments public partial class Form1 : ... Pin
charles henington6-Apr-11 3:55
charles henington6-Apr-11 3:55 
GeneralReason for my vote of 5 Very clean code; would be nice to ha... Pin
DrABELL5-Apr-11 14:45
DrABELL5-Apr-11 14: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.