send email in vb .net code example

Example: send email from vb.net

'You will need the following tools:
'5 textboxes (txtFrom, txtPass, txtTo, txtSubject, txtMessage)
'5 labels (labelling each of the text boxes)
'2 buttons (btnSend, btnExit)

Imports System.Net.Mail

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtPass.PasswordChar = Chr(149)
    End Sub

    Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
        Try
            Dim mail As New MailMessage
            'this program will send an email to another user via Outlook
            'as shown below
            Dim smtpServer As New SmtpClient("smtp.outlook.com")
            
            'you can change 'outlook' with another email sending website, such as Gmail
            mail.From = New MailAddress(txtFrom.Text)
            mail.To.Add(txtTo.Text)
            mail.Subject = txtSubject.Text
            mail.Body = txtMessage.Text
			
            'checking the user's details
            smtpServer.Port = 587
            smtpServer.Credentials = New System.Net.NetworkCredential(txtFrom.Text, txtPass.Text)
            smtpServer.EnableSsl = True
            smtpServer.Send(mail)

			'confirmation message to the user
            MsgBox("Your email has been sent", MessageBoxIcon.Information, MessageBoxButtons.OK)
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical)
        End Try
    End Sub

	'exit button
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class

Tags:

Vb Example