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
    }
}

How to Send Emails from Google SMTP

I came into an issue where I need to send an email from my website.. I don’t have an SMTP service installed on my web server and I didn’t want to go into that path for many of my personal reasons. so I decided to go with Google SMTP service as it is free as well as it is accessible from anywhere.. to accomplish this task it took from me 15 minutes.. so I thought of sharing my code with you as well

public void SendEmail ()
{

var client = new SmtpClient(“smtp.gmail.com”, 587)
{

Credentials = new NetworkCredential(“EmailAccountUserName@gmail.com”, “Password”),
EnableSsl = true;

};

client.Send(“EmailAccountUserName@gmail.com”, “EmailAccountUserName@gmail.com”, “EmailSubject”, “EmailBody”);

}

I hope you will find this useful…

I hope