Click here to Skip to main content
12,817,318 members (33,902 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 6 Apr 2011

Using Gmail Account to Send Emails With Attachment

, 18 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Sending mail with attachmentspublic partial class Form1 : Form { public Form1() { InitializeComponent(); } public class MailClass : List { public void CreateMailAddresses(string sender, string reciever, out...
Sending mail with attachments

public partial class Form1 : Form
        public Form1()

        public class MailClass : List<string>
            public 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 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 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;
        MailClass Mail = new MailClass();
        private void button1_Click(object sender, EventArgs e)
            MailAddress mailfrom;
            MailAddress mailto;
            Mail.CreateMailAddresses("", "", out mailfrom, out mailto);
            MailMessage newmsg = Mail.MailMessage(mailfrom, mailto, "Subject of Email", "Body(message) of email");
            Byte google;
            for (google = 0; google < Mail.Count; google++)
                newmsg.Attachments.Add(new Attachment(Mail[google]));
            SmtpClient smtp = Mail.SmtpClient("", "password", "", true, 587);

        private void button2_Click(object sender, EventArgs e)
            // add attachments
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.Cancel)


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


About the Author

charles henington
United States United States
I do not claim to be wrong! I just rarely ever write.

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 3 CreateMailAddresses function is not ... Pin
Philippe Mori14-May-11 11:27
memberPhilippe Mori14-May-11 11:27 
GeneralReason for my vote of 5 wow attachments to Pin
cSNewb22-Apr-11 7:03
membercSNewb22-Apr-11 7:03 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170308.1 | Last Updated 18 Apr 2011
Article Copyright 2011 by charles henington
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid