Using clsSendGmail.cs to Send Email Via Google Mail


This entry in my collection of "C# Errata" presents a class I wrote, "clsSendGmail.cs" (downloadable here). The class is shown below. Following the listing of the class source code, I show a screenshot and download link for a sample application I built demonstrating how to leverage this class. I make a setup file for that application available, so that you can see the class in action.

What's more, I'm making the entire source code project available for download, so that anyone who wants can study how the application interacts with the class to obtain the desired functionality.


using System;
using System.Net;
using System.Net.Mail;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Kyrathasoft.NetUtilities {

    class clsSendGmail {       

        //default constructor
        public clsSendGmail() {
            this.oMailMsg = new MailMessage();
        }

        
        public clsSendGmail(string username, string password, string recipient, string body,            
            bool bodyHTML, string subject, List attachments) {
            
            //an overload of the default constructor

                this.oMailMsg = new MailMessage(username + "@gmail.com", recipient, subject, body);            
            
            if (bodyHTML) { oMailMsg.IsBodyHtml = true; }
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.Timeout = 100000;
            client.EnableSsl = true;

            NetworkCredential nc = new NetworkCredential(username, password);
            client.Credentials = nc;
           
            if (attachments.Count > 0) {
                foreach (Attachment attachment in attachments) {
                    oMailMsg.Attachments.Add(attachment);
                }
            }
            client.SendCompleted += new SendCompletedEventHandler(MailDeliveryComplete);
            client.SendAsync(this.oMailMsg, "sending");
        }

        //private member variable of clsSendMail
        private MailMessage oMailMsg;

        //method belonging to clsSendMail
        private void MailDeliveryComplete(object sender, System.ComponentModel.AsyncCompletedEventArgs e) {
            
            string s = e.ToString();
   
            if (e.Error != null) {
                s = "Error sending email: please double-check the username, password, and recipient's email ";
                s += "address, then try sending the email again.";
            }
            else if (e.Cancelled) {
                s = "Sending of email cancelled.";
            }
            else {
                s = "Your email message was sent successfully.";
            }

            MessageBox.Show(s, "Mail Completed Status");
        }
        //next right-brace ends clsSendMail
    }
    //next right-brace ends namespace Kyrathasoft.NetUtilities
}
And here is a screenshot of the sample application I created, leveraging the above-listed class (clicking on the screenshot will prompt a download of the setup file for this sample application). If you'd prefer to do a manual install (just copying the raw executable and dependency files without doing any automated install that involves Windows Registry writes), you can download that here.