How to send emails to multiple users from a VB.NET application ?


Sending emails to multiple people, on click of a single button on a desktop application software can be quite handy in many situations. Here, I will discuss the steps to send email from a vb.net application form on click of a button named as Button1. One just needs to know the smtp port number and and the smtp client name of the email service that is to be used, and ofcourse the email address and password of the sender. In this article, I am using a gmail account to send emails.

Sending emails to multiple people, on click of a single button on a desktop application software can be quite handy in many situations.

Here, I will discuss the steps to send email from a vb.net application form on click of a button named as Button1. One just needs to know the smtp port number and and the smtp client name of the email service that is to be used, and ofcourse the email address and password of the sender. In this article, I am using a gmail account to send emails.


First step would be to import these two namespaces in the form that has the button control.

Imports System.Net.Mail
Imports System.Net


On the button click event, write the following code.




Try
Dim AnEmailMessage As New MailMessage
AnEmailMessage.From = New MailAddress("Sender's email address")
AnEmailMessage.To.Add(" ") 'add the email addresses separated by a comma
AnEmailMessage.Subject = (" ") 'you can add a subject here
AnEmailMessage.Body = (" ") 'add the body of the mail
AnEmailMessage.Attachments.Add(New System.Net.Mail.Attachment("")) 'add attachment
AnEmailMessage.Priority = MailPriority.High
Dim SimpleSMTP As New SmtpClient("smtp.gmail.com")
With SimpleSMTP
.Port = 587
.EnableSsl = True
.Credentials = _
New NetworkCredential("Sender's email address ", "password") 'username and passwrd
.Send(AnEmailMessage)
End With
MsgBox("Email sent !", MsgBoxStyle.Information)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub



Adding attachments to the email is not compulsory. In case you want to add any, u can do so by just mentioning the path between double quotes . Smtp client name for gmail is "smtp.gmail.com" and its port number is 587.


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: