In this article we will learn about some of the frequently asked Visual Basic programming questions in technical like “send email from vb.net” Code Answer. When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks. Error handling in VBA is simple. An error message with filename, line number and a message describing the error is sent to the browser. This tutorial contains some of the most common error checking methods in Visual Basic. Below are some solution about “send email from vb.net” Code Answer.
send email from vb.net
xxxxxxxxxx
1
'You will need the following tools:
2
'5 textboxes (txtFrom, txtPass, txtTo, txtSubject, txtMessage)
3
'5 labels (labelling each of the text boxes)
4
'2 buttons (btnSend, btnExit)
5
6
Imports System.Net.Mail
7
8
Public Class Form1
9
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
10
txtPass.PasswordChar = Chr(149)
11
End Sub
12
13
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
14
Try
15
Dim mail As New MailMessage
16
'this program will send an email to another user via Outlook
17
'as shown below
18
Dim smtpServer As New SmtpClient("smtp.outlook.com")
19
20
'you can change 'outlook' with another email sending website, such as Gmail
21
mail.From = New MailAddress(txtFrom.Text)
22
mail.To.Add(txtTo.Text)
23
mail.Subject = txtSubject.Text
24
mail.Body = txtMessage.Text
25
26
'checking the user's details
27
smtpServer.Port = 587
28
smtpServer.Credentials = New System.Net.NetworkCredential(txtFrom.Text, txtPass.Text)
29
smtpServer.EnableSsl = True
30
smtpServer.Send(mail)
31
32
'confirmation message to the user
33
MsgBox("Your email has been sent", MessageBoxIcon.Information, MessageBoxButtons.OK)
34
Catch ex As Exception
35
MsgBox(ex.Message, vbCritical)
36
End Try
37
End Sub
38
39
'exit button
40
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
41
Me.Close()
42
End Sub
43
End Class