24. Sending An Email

In this tutorial, we will learn how to send an email using only code. The example in this tutorial will show you how to send an email by connecting to the googlemail SMTP server. If you want to send an email using another service such as hotmail or yahoomail, you will need to google for the SMTP host and port for that particular service.


Imports System.Net.Mail

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Mail As New MailMessage
        Mail.Subject = "test email"
        Mail.From = New MailAddress("youremail@googlemail.com")
        Mail.Body = "This is an ownage email using VB.NET"

        Dim SMTP As New SmtpClient("smtp.gmail.com")
        SMTP.EnableSsl = True
        SMTP.Credentials = New System.Net.NetworkCredential("username", "password")
        SMTP.Port = "587"
    End Sub

End Class