Home » Applications » VB.NET – Send Email

VB.NET – Send Email

Send email using web form – user defines smtp settings.

send-email.aspx
 
To: 
From: 
SMTP Server: 
Subject: 
Attachment: 
Body: 

Action: 

Status: [labelEmailSent]

send-email.aspx.vb

Imports System.Net.Mail

Partial Class Send_Email
    Inherits System.Web.UI.Page
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
                                                              Handles btnSubmit.Click
        Try
            Dim SendFrom As MailAddress = New MailAddress(txtFrom.Text)
            Dim SendTo As MailAddress = New MailAddress(txtTo.Text)

            Dim MyMessage As MailMessage = New MailMessage(SendFrom, SendTo)

            MyMessage.Subject = txtSubject.Text
            MyMessage.Body = txtBody.Text

            Dim attachFile As New Attachment(txtAttachmentPath.Text)
            MyMessage.Attachments.Add(attachFile)

            Dim emailClient As New SmtpClient(txtSMTPServer.Text)
            emailClient.Send(MyMessage)

            litStatus.Text = "Message Sent"
        Catch ex As Exception
            litStatus.Text = ex.ToString()
        End Try
    End Sub
End Class