E-mail Alerts from VB application

Sending an e-mail via VB.Net can be done easily with the following code. You can also modify the code to send e-mail via an ASP web page.
Add this line in your declarations area.
Imports System.Net.Mail
Next, create a subroutine by entering the following code:
Public Sub SendEmail(ByVal strTo As String, ByVal strSubject As String, ByVal strMessage As String)

On Error GoTo SEerrTrap
Dim emailMsg As New MailMessage("fromEmail@wherever.com", strTo, strSubject, strMessage)

'Settings for using G-mail. Replace the url with the url or IP address of your mail server
'and the port number with "25" for an inhouse mail server.
Dim emailclient As New SmtpClient("smtp.gmail.com", "587") 

emailclient.EnableSsl = True 'Enables SSL. You likely will not need this unless you are using a G-Mail account to send mail.
emailMsg.IsBodyHtml = True 'Enables HTML e-mail bodies
emailMsg.Priority = MailPriority.High 'Sets the priority of the e-mail and how it appears on the recipients PC.
emailclient.Credentials = New System.Net.NetworkCredential("userID", "password") 'User ID and password for the SMTP mail server

Exit Sub
     MsgBox(Now & " Error " & Err.Number & " " & Err.Description & " in SendEmail")
End Sub

Calling the subroutine with the appropriate variables included will send the e-mail via your SMTP server specified in the code.

Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: