How to Send an Email in C# using gmail

Send an Email

It is becoming common these days to send an email from your application to specific list of users. Usually this is to notify users or administrators of certain action happened. As example of this, Notify administrator if an error happened in certain module, or notifying manager to approve/decline a request.

Thankfully, sending emails from .net applications (desktop or web) is becoming easy task to accomplish. Here is a sample code that utilizing gmail smtp to send an email. You can use the same steps to send email from outlook.com or office365.com, you will need to change the configurations in emailSettingInfo object

public void SendEmail()
{
    string sender, recipient, ccList, bccList, smtpSenderPassword;
    //Prepare SMTP Setting
    var emailSettingInfo = new
    {
        SmtpSenderEmail = "<<Sending Email Address Goes here>>",
        SmtpSenderPassword = "<<Sending Email Password Goes here>>",
        SmtpClientHost = "smtp.gmail.com",
        SmtpPortNumber = 587,
        SmtpEnableSsl = true
    };

    //Get the Email body ready
    var emailInfo  = new {
        ToEmailAddress = "<<Your email goes here>>",
        Subject = "Send Email from C# Code",
        Body = "Hi, this is a test email",
        CcEmailAddress = "<<CC List>>",
        BccEmailAddress = "<<Bcc List>>",
    };

    sender = emailSettingInfo.SmtpSenderEmail;
    recipient = emailInfo.ToEmailAddress;
    smtpSenderPassword = emailSettingInfo.SmtpSenderPassword;

    //Intialise Parameters  
    SmtpClient client = new SmtpClient(emailSettingInfo.SmtpClientHost);
    client.Port = emailSettingInfo.SmtpPortNumber;
    client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(sender, smtpSenderPassword);
    client.EnableSsl = emailSettingInfo.SmtpEnableSsl;
    client.Credentials = credentials;
    try
    {
        var mail = new System.Net.Mail.MailMessage(sender.Trim(), recipient.Trim());

        mail.Subject = emailInfo.Subject;
        mail.Body = emailInfo.Body;
        mail.IsBodyHtml = true;
        if (!string.IsNullOrEmpty(emailInfo.CcEmailAddress ))
        {
            ccList = emailInfo.CcEmailAddress;
            foreach (var email in ccList.Split(";".ToArray()))
            {
                mail.CC.Add(new MailAddress(email));
            }
        }

        if (!string.IsNullOrEmpty(emailInfo.BccEmailAddress ))
        {
            bccList = emailInfo.BccEmailAddress;
            foreach (var email in bccList.Split(";".ToArray()))
            {
                mail.Bcc.Add(new MailAddress(email));
            }
        }

        client.SendCompleted += (s, e) => {
            client.Dispose();
            mail.Dispose();
        };

        client.Send(mail);//send the email
                
    }
    catch (Exception ex)
    {
        //Do Erorr Handling logic
    }
}