How To Use Google SMTP Server To Send Email

Introduction

This article explains how you can simply configure Google's SMTP server settings into whatever script or program you wish to use. All you need to have is a Gmail account. Not only Gmail, but the user can use this service for different domains. All the user needs to have is an account with that server and needs to pass values according to that server. Please find the executable downloadable project.

Modules

  • Create a Windows Forms Application using Visual Studio.
  • Create User Interface.
  • Add necessary code to finish the development.

Technologies Used

  • Windows forms application
  • C# v5

References

IDE

  • Visual Studio 2013
Module 1: Create a Windows Forms Application using Visual Studio
Design 
 

Module 2: Create User Interface

  • Add a new Form to the project.
  • Drag and drop necessary fields to the form from "Toolbox".
  • Change the necessary properties(name, id, etc.,) required for the fields.

Module 3: Add necessary code to finish the development

  • This project uses C# for development.
  • Add necessary methods required to complete the development.

Using the code

  1. /// <summary>  
  2. /// This method is used to attach any files/images/etc.. to the email  
  3. /// </summary>  
  4. /// <param name="sender">sender</param>  
  5. /// <param name="e">e</param>  
  6. private void btnAttach_Click(object sender, EventArgs e)  
  7. {  
  8.     OpenFileDialog openDialogue = new OpenFileDialog();  
  9.     if (openDialogue.ShowDialog() == DialogResult.OK)  
  10.     {  
  11.         attachmentsListBox.Items.Add(openDialogue.FileName);  
  12.     }  
  13. }  
  14.   
  15. /// <summary>  
  16. /// This method is used to remove selected items from the attachments  
  17. /// </summary>  
  18. /// <param name="sender">sender</param>  
  19. /// <param name="e">e</param>  
  20. private void btnRemove_Click(object sender, EventArgs e)  
  21. {  
  22.     var item = attachmentsListBox.SelectedItem;  
  23.     attachmentsListBox.Items.Remove(item);  
  24. }  
  25.   
  26. /// <summary>  
  27. /// This method is used to authenticate and send email. It throws exception if the   
  28. /// information passed is wrong and the error can be seen on label3  
  29. /// </summary>  
  30. /// <param name="sender">sender</param>  
  31. /// <param name="e">e</param>  
  32. private void btnSend_Click(object sender, EventArgs e)  
  33. {  
  34.     try  
  35.     {  
  36.         if (ValidateForm())  
  37.         {  
  38.             label3.Text = "";  
  39.             MailMessage mailMessage = new MailMessage(username.Text, toTextBox.Text);  
  40.             mailMessage.Subject = subjectTextBox.Text;  
  41.             mailMessage.Body = bodyTextArea.Text;  
  42.             AddItems(ccTextBox.Text, mailMessage, "CC");  
  43.             AddItems(bccTextBox.Text, mailMessage, "BCC");  
  44.             for (int i = 0; i < attachmentsListBox.Items.Count; i++)   
  45.             {  
  46.                 mailMessage.Attachments.Add(new Attachment(attachmentsListBox.Items[i].ToString()));  
  47.             }  
  48.             SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);  
  49.             smtpClient.Credentials = new System.Net.NetworkCredential(username.Text, password.Text);  
  50.             smtpClient.EnableSsl = true;  
  51.             mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;  
  52.             smtpClient.Send(mailMessage);  
  53.         }  
  54.     } catch (Exception ex)  
  55.     {  
  56.         label3.Text = ex.Message;  
  57.     }  
  58. }  
  59.   
  60.   
  61. /// <summary>  
  62. /// This method is used to add emails to "CC" and "Bcc" if any  
  63. /// </summary>  
  64. /// <param name="emailString">emailstring</param>  
  65. /// <param name="mailMessage">mailMessage</param>  
  66. /// <param name="type">type</param>  
  67. private void AddItems(string emailString, MailMessage mailMessage, string type)  
  68. {  
  69.     if (!string.IsNullOrEmpty(emailString))  
  70.     {  
  71.         ArrayList emailList = new ArrayList();  
  72.         String[] emails = emailString.Split(',');  
  73.         if (type == "CC")  
  74.         {  
  75.             for (int i = 0; i < emails.Length; i++)  
  76.                 mailMessage.CC.Add(new MailAddress(emails[i]));  
  77.         } else if (type == "BCC")  
  78.         {  
  79.             for (int i = 0; i < emails.Length; i++)  
  80.                 mailMessage.Bcc.Add(new MailAddress(emails[i]));  
  81.         }  
  82.     }  
  83. }  
  84.   
  85. /// <summary>  
  86. /// This method is used to check if all the mandatory fields are entered.  
  87. /// It returns true if all the fields are entered else returns false.  
  88. /// </summary>  
  89. /// <returns>bool</returns>  
  90. private bool ValidateForm()   
  91. {  
  92.         if (string.IsNullOrEmpty(username.Text) || string.IsNullOrEmpty(password.Text) || string.IsNullOrEmpty(toTextBox.Text))   
  93.         {  
  94.             label3.Text = "* fields are mandatory";  
  95.             return false;  
  96.         }  
  97.         return true;  
  98.     } //  

Note:

  • The user can download the project and use it as a desktop application. All the user needs to do is download the project, compile it, and create a shortcut using .exe from bin -> debug->WindowsFormsApplication1.exe.

  • Sometimes it may not work because it needs user permission to turn on access. The user can turn on by visiting here.

Below is the screenshot of how the error looks:

error

Below is the screenshot of how it looks. The user can turn on by selecting Turn on radio button,

screenshot

 
Read more articles on  Windows Forms:

Up Next
    Ebook Download
    View all
    Learn
    View all