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

Technologies Used

References

IDE

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

Module 2: Create User Interface

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

Note:

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: