article

Send an E-mail from VB.NET

Email
Submitted on: 1/1/2015 6:08:00 AM
By: Robson Mendonša (from psc cd)  
Level: Intermediate
User Rating: By 5 Users
Compatibility: VB.NET
Views: 546
 
     Here is a code to send an e-mail from a Winform made in VB.NET. Check it!

 
				
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


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.