README.md

December 4, 2022 · View on GitHub

Send an E-mail from VB.NET

Description

Here is a code to send an e-mail from a Winform made in VB.NET. Check it!

More Info

Submitted On
ByRobson Mendonça
LevelIntermediate
User Rating4.6 (23 globes from 5 users)
CompatibilityVB.NET
CategoryInternet/ Browsers/ HTML
World.Net (C#, VB.net)
Archive File

Source Code


To send an e-mail in VB.NET, we can use the System.Web.Mail class. See the code below:


Imports System.Web.Mail

Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents TextBox3 As System.Web.UI.WebControls.TextBox
Protected WithEvents Label4 As System.Web.UI.WebControls.Label
Protected WithEvents TextBox4 As System.Web.UI.WebControls.TextBox
Protected WithEvents RadioButton2 As System.Web.UI.WebControls.RadioButton
Protected WithEvents RadioButton1 As System.Web.UI.WebControls.RadioButton
Protected WithEvents Button1 As System.Web.UI.WebControls.Button

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If RadioButton1.Checked Then
Dim simplemail As New MailMessage()
Dim from_, to_, title_, message_ As String
from_ = TextBox1.Text.ToLower.Trim()
to_ = TextBox2.Text.ToLower.Trim()
title_ = TextBox3.Text.Trim()
message_ = TextBox4.Text.Trim()
SmtpMail.Send(from_, to_, title_, message_)
Else
Dim AttachEmail As New MailMessage()
With AttachEmail
.From = TextBox1.Text.ToLower.Trim
.To = TextBox2.Text.ToLower.Trim
.Subject = TextBox3.Text.Trim
.Body = "" & TextBox4.Text.Trim() & ""
.BodyFormat = MailFormat.Html
.Priority = MailPriority.High
.Attachments.Add(new MailAttachment("c:\attach.txt"))
End With
SmtpMail.Send(AttachEmail)
End If
End Sub
End Class