Send An Email To Gmail Using SMTP In Windows Application

In this article you will learn how to send email to Gmail with the help of SMTP in windows application using C#.

In this article I will show you how to send an email to gmail with the help of SMTP in windows application using C#.

Initial chamber

Step 1: Open Your Visual Studio 2010 and Go to File, New, then Projects and under Visual C# select Windows.

Windows form application

You can change the name of the project and you can browse your project to different location too. And then press – OK.

Design chamber

Step 2: Now open your Form1.cs file, where we will create our design for sending an email to gmail. We will drag 3 labels, 3 textboxes and one button from toolbox to Form1.cs. You will see your form look like the following:

Design

Code chamber

Right click on the blank part of Form1.cs and View Code. You will see you are entered in the code part of the form. Write the following code and then Press F5 to run the project.

Add some of the namespaces for sending Gmail.

namespaces

If you can’t find these namespaces in the IntelliSense, then in the Solution Explorer - Right Click and Add References. In Add References, .NET, find “System.Net”and Press Ok.Now IntelliSense will surely show you.

Add References

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Net;  
  10. using System.Net.Mail;  
  11.   
  12. namespace wingmailapplication  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.         private void button1_Click(object sender, EventArgs e)  
  22.         {  
  23.             try  
  24.             {  
  25.   
  26.                 MailMessage msg = new MailMessage();  
  27.                 msg.From = new MailAddress("nilusilu3@gmail.com");  
  28.                 msg.To.Add(textBox1.Text);  
  29.                 msg.Subject = textBox3.Text;  
  30.                 msg.Body = textBox2.Text;  
  31.   
  32.                 SmtpClient smt = new SmtpClient();  
  33.                 smt.Host = "smtp.gmail.com";  
  34.                 System.Net.NetworkCredential ntcd = new NetworkCredential();  
  35.                 ntcd.UserName = "nilusilu3@gmail.com";   
  36.                 ntcd.Password = "";  
  37.                 smt.Credentials = ntcd;  
  38.                 smt.EnableSsl = true;  
  39.                 smt.Port = 587;  
  40.                 smt.Send(msg);  
  41.   
  42.                 MessageBox.Show("Your Mail is sended");  
  43.   
  44.             }  
  45.             catch (Exception ex)  
  46.             {  
  47.   
  48.                 MessageBox.Show(ex.Message);  
  49.             }  
  50.              
  51.   
  52.         }  
  53.     }  
  54. }  
Output chamber

Output

It will show an error if you are not having internet connection or if you haven't fill all the textboxes or any other.

fill all the textboxes

Lets check Gmail and you can see our email there. If you will find it, that means email is sent properly.

check on Gmail

Hope you liked it. Have a good day. Thank you for reading.