Sample code to send email via gmail in c#

For some time, I’ve had a block of code that I copied and pasted all over the place, to send emails via gmail. It didn’t add an attachment, but I needed to do so today. So rather than copy and paste it yet again, I’ve created a simple static class that can be reused.

Note that:

  1. I hard-code my gmail address (the username) and my password as constants. (Changed to invalid values here.) You’ll need to change them to something valid.
  2. When debugging, I don’t use gmail. I use an application called Papercut, and there is a similar one called smtp4dev I think… to mock a local SMTP server. It then “receives” the email.
  3. I don’t do anything if there is an exception when trying to send the email. That’s because the applications using this code typically send emails from their exception handlers when there are errors. You might want to do something different.
  4. I’m not going to explain how to enable SMTP on a gmail account. I trust that anybody using this can figure that out for themselves.

This is really simple code, and I consider it junior developer level. I’m just putting it here, to make it easier to reuse. Please don’t ask me any questions about it. It does the basic job I need it to do and that’s all.

I’ll paste an example that uses it below…

using System.Collections.Generic;
using System.Net.Mail;

namespace Example
    public static class EmailHandler
        private const string UserName = "";
        private const string Password = "password123%";

        public static void SendEmail(IEnumerable<string> toAddresses, IEnumerable<string> copyAddresses, string subject, string from, string body, System.Net.Mail.Attachment attachment = null, bool isHtml = true)
            using (MailMessage mailMessage = new MailMessage())
                foreach (var to in toAddresses)

                foreach (var copy in copyAddresses)

                mailMessage.Subject = subject;

                mailMessage.From = new MailAddress(from);
                mailMessage.Body = body;
                mailMessage.IsBodyHtml = isHtml;

                if (attachment != null)

                    using (SmtpClient smtp = new SmtpClient(""))
                        smtp.EnableSsl = false;
                        smtp.Port = 25;
                            using (SmtpClient smtp = new SmtpClient("", 587)
                                Credentials = new System.Net.NetworkCredential(UserName, Password),
                                EnableSsl = true
                catch { }

In my example that uses the above, I’m calling it from ASP.Net. The attachment I pass to it is a PDF rendered from an SSRS report, using the ReportViewer control. You shouldn’t really need this code, except for the part that initializes the attachment, because it may not be obvious how to do that. The attachment can be initialized via a file name or stream, and needs a MIME type. I use the constructor that takes a stream, a file name, and the content type name. Bizarrely there is also one that takes a content type itself and a stream, without a file name. That one is kind of shitty.

        protected void cbSendEmail_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
            string[] toEmails = ((string)flEmail.GetNestedControlValueByFieldName("ToEmail")).Replace(";", ",").Split(',');

            if (toEmails.Length > 0)
                string subject = (string)flEmail.GetNestedControlValueByFieldName("Subject");

                string body = htmlMessage.Html;

                int id = (int)Session["InvoiceID"];
                ReportParameter p = new ReportParameter("InvoiceID", id.ToString());

                reportViewer.ServerReport.SetParameters(new ReportParameter[] { p });
                byte[] data = reportViewer.ServerReport.Render("pdf");

                using (MemoryStream stream = new MemoryStream(data))
                    System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(stream, "Invoice.pdf", System.Net.Mime.MediaTypeNames.Application.Pdf);
                    EmailHandler.SendEmail(toEmails, Enumerable.Empty<string>(), subject, "", body, attachment);

About Jerome

I am a senior C# developer in Johannesburg, South Africa. I am also a recovering addict, who spent nearly eight years using methamphetamine. I write on my recovery blog about my lessons learned and sometimes give advice to others who have made similar mistakes, often from my viewpoint as an atheist, and I also write some C# programming articles on my programming blog.
This entry was posted in Programming and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s