How to send a simple email message [VB.NET]

A simple VB.NET code for sending email.

The System.Net.Mail namespace contains MailMessage and SmtpClient classes, the System.Net namespace contains NetworkCredential class, that we need in order to send the email and specify the user credentials necessary to send authenticated email.

This example will show you how to send a simple email message with authentication using VB.NET:

Imports System.Net 
Imports System.Net.Mail 

Public Class MailSender 
    Public Shared Sub SendMail(ByVal from As String, ByVal [to] As String, ByVal subject As String, ByVal body As String, ByVal smtpServer As String, ByVal smtpUsername As String, _ 
    ByVal smtpPassword As String) 
        Dim message As New MailMessage(from, [to], subject, body) 
        Dim smtpClient As New SmtpClient(smtpServer) 
        smtpClient.UseDefaultCredentials = False 
        Dim credentials As New NetworkCredential(smtpUsername, smtpPassword) 
        smtpClient.Credentials = credentials 
    End Sub 
End Class

Applies To

A simple VB.NET code for sending email.

See Also