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

This tutorial shows you how to send mail using gmail smtp server in C#.NET 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.

You can create a new windows forms application project, then enter your project name is "SendEmail", and then click OK button.

Drag TextBox, Label, CheckBox, GroupBox and Button 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
    NetworkCredential login = new NetworkCredential(txtUsername.Text, txtPassword.Text);
    SmtpClient client = new SmtpClient(txtSmtp.Text);
    client.Port = Convert.ToInt32(txtPort.Text);
    client.EnableSsl = chkSSL.Checked;
    client.Credentials = login;
    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.

VIDEO TUTORIALS