How Can We Help?
How to send email from VB.NET?
In the last tutorial, you are going to learn how to send email in VB.NET using SMTP protocol. At the end of this tutorial, you will come to know how to set up SmtpClient class so that you contact your email server.
What is SMTP ?
SMTP stands for Simple Mail Transfer Protocol. In layman terms it is like a small language that consists of only a few words. This language is used by the sender of the email to send an email to the receiver of the email. In the real world, both the sender and the receiver are actually an application or so-called computer programs. In technical terms, the sender is called SMTP client and the receiver is called as SMTP server. SMTP is a standard language (protocol) used by all the applications on this planet to send emails. However, the same cannot be used for receiving emails. For receiving one needs to use POP or IMAP.
How do I send email using VB.Net?
VB.NET using SMTP protocol for sending the email . VB.NET using System.Net.Mail namespace for sending the email. You need to instantiate SmtpClient class and assign the Host and Port. The default port using SMTP is 25, but it may vary different Mail Servers.
First, you need to add the following to the top of your code, above the Class line:
Imports System.Net Imports System.Net.Mail Imports System.Net.Sockets Imports System.IO Imports System.Text
The point about adding these import lines is so that you don’t have to write long lines of code like System.Net.Mail.SmtpClient. Because the namespaces have been added to the top, you can just write SmtpClient instead.
Now you have set the sender details like To-Email, From-Email, Subject, and Body.
Dim emailTo As String = ToEmailAddresses Dim emailFrom As String = Set from email Dim emailSubject As String = Set Subject line Dim emailBody As String = Set email content
It will be good practice here to add error checking if you like (and you should). We’ll leave it out, though, for convenience’s sake.
To send emails, there is a class called SmtpClient. This is on the Net.Mail namespace. So, in VB Net, add the following to your code
Dim smtpServer As New SmtpClient
This sets up an object of type SmtpClient that we’ve called smtpServer.
The SmtpClient needs the following details from you:
SMTP host address SMTP port SMTP username SMTP password
OK, now that we have all the details, now you can set up your SmtpClient.
Add the Host with this line of code:
smtpServer.Host = "smtp.my_isp.net"
Obviously, you should substitute smtp.my_isp.net for whatever your Host is.
You can also set a TimeOut:
smtpServer.Timeout = 60
The TimeOut property is in seconds and is the length of time that Visual Studio will wait before giving up sending the email.
If your Port number is not 25, then you can set it with this:
smtpServer.Port = 2525 or 587
As you know, Port 25 is the default, so you don’t need this line, unless it’s not 25. But it’s best to set it anyway, in case Microsoft change the default.
You also set a delivery method for your email:
smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
There are only two other options: Pickup directory from IIS, and Specified Pickup directory. We’re assuming that you want to send your email through the Network directly to a SMTP server.
You also need to specify our SMTP login credentials. Set up two string variables for this:
Dim username As String = "my_mail250_username" Dim password As String = "my_mail250_password"
The SmtpClient object has a Credentials property. This needs a NetworkCredential object. You set it up like this:
smtpServer.Credentials = New NetworkCredential( username, password )
In between the round brackets of NetworkCredential, you need your username and password to log on to your SMTP server.
Your code should now look something like this:
Imports System.Net Imports System.Net.Mail Imports System.Net.Sockets Imports System.IO Imports System.Text Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Try Dim SmtpServer As New SmtpClient() Dim mail As New MailMessage() SmtpServer.Credentials = New _ Net.NetworkCredential("[email protected]", "password") SmtpServer.Port = 587 SmtpServer.Host = "smtp.my_isp.com" SmtpServer.Timeout = 60 mail = New MailMessage() mail.From = New MailAddress("[email protected]") mail.To.Add("TOEmailADDRESS") mail.Subject = "Test is the Mail" mail.Body = "This is for testing SMTP mail from GMAIL" SmtpServer.Send(mail) MsgBox("mail send") Catch ex As Exception MsgBox(ex.ToString) End Try End Sub End Class
Open Source Tools: 20 BEST Website Monitoring Tools Open source