Windows Forms: How to send mail using Gmail SMTP Server in C#

This post shows you How to send mail using gmail smtp server in C# Windows Forms Application.

Sending email via Gmail SMTP server with c# is quite simple. First, you need to understand briefly about smtp server.

SMTP is part of the application layer of the TCP/IP protocol. It's an internet standard for email transmission. The default TCP port used by SMTP is 25 and SMTP connections are secured by SSL, called SMTPS, using the default for port 465.

SMTP provides a set of protocols that simplify email communication between mail servers. Most SMTP server names are written as "smtp.domain.com" or "mail.domain.com".

For example, a Gmail account will access smtp.gmail.com. It's often used with one of two other protocols, POP3 or IMAP, allowing users to store messages in the server's mailbox and periodically download from the server.

How to send email in C#

To send an email using Gmail SMTP server in C#, you can use the SmtpClient class available in the System.Net.Mail namespace.

Here's a basic example of how you can achieve this.

You can create a new Windows Forms Application project, then enter your project name is "SendEmail", and then click OK button.

Drag and drop TextBox, Label, CheckBox, GroupBox and Button controls from your Visual Studio toolbox to your winform, then design a simple UI that allows you to send send mail using smtp server in c# as shown below.

how to send mail using gmail smtp server in c#

Add the event handler code to the send button to send email gmail in c# as shown below.

private void btnSend_Click(object sender, EventArgs e)
{
    //login to your mail server, set the credentials (username and password) for the sender
    NetworkCredential login = new NetworkCredential(txtUsername.Text, txtPassword.Text);
    // Create a new SmtpClient instance
    SmtpClient client = new SmtpClient(txtSmtp.Text);
    // Set the SMTP port to 587
    client.Port = Convert.ToInt32(txtPort.Text);
    client.EnableSsl = chkSSL.Checked;
    client.Credentials = login;
    // Create a new MailMessage object
    MailMessage  msg = new MailMessage { From = new MailAddress(txtUsername.Text + txtSmtp.Text.Replace("smtp.", "@"), "FoxLearn", Encoding.UTF8) };
    msg.To.Add(new MailAddress(txtTo.Text));
    if (!string.IsNullOrEmpty(txtCC.Text))
        msg.To.Add(new MailAddress(txtCC.Text));
    msg.Subject = txtSubject.Text;
    msg.Body = txtMessage.Text;
    msg.BodyEncoding = Encoding.UTF8;
    msg.IsBodyHtml = true;
    msg.Priority = MailPriority.Normal;
    msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
    client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
    string userstate = "Sending...";
    //Send email async
    client.SendAsync(msg, userstate);
}

Add code to SendCompleted event handler when finish sending email in c# code.

private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
    if (e.Cancelled)
        MessageBox.Show(string.Format("{0} send canceled.", e.UserState), "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
    if (e.Error != null)
        MessageBox.Show(string.Format("{0} {1}", e.UserState, e.Error), "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
    else
        MessageBox.Show("Your message has been successfully sent.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

To fix "the smtp server requires a secure connection or the client was not authenticated", you should set SSL to true.

You may need to enable "Less Secure Apps" in your Gmail account settings or use an App Password if you have two-factor authentication enabled on your account.

VIDEO TUTORIALS