How to send email using VB.NET code
By: Issac
Surprised, we can use SMTP protocol and .net source code for sending an email, SMTP stands for Simple Mail Transfer Protocol, in VB.NET we can use System.Net.Mail namespace for send mail. We can instantiate SmtpClient class and assign the Host and Port. The default port using SMTP is 25, but it may vary different Mail Servers. The following example shows how to send emails from a Gmail address.
The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587. Here using NetworkCredential for password based authentication.
Imports System.Net.Mail
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.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("[email protected]")
mail.To.Add("TOADDRESS")
mail.Subject = "Test 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
You have to provide the informations like your gmail username and password , and the to address also.
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net
Exception Filtering in the Catch Block in VB.net
Using Multiple Catch Statements in VB.net
Throwing an Exception in VB.net
Throwing a Custom Exception in VB.net
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net
Archived Comments
1. nicely working please check it with following URL<
View Tutorial By: darshana at 2010-09-28 11:11:35
2. It won't work for me ;S
View Tutorial By: jordi at 2011-01-21 08:22:45
3. I get an error returned saying the SMPT server req
View Tutorial By: Christian Peut at 2011-04-04 04:35:51
4. Imports System.Web
Imports System.Net.Mail<
View Tutorial By: Naresh at 2011-05-24 07:27:12
5. "I get an error returned saying the SMPT serv
View Tutorial By: Dennis at 2011-06-24 10:18:10
6. Thanks a lot
View Tutorial By: praveen at 2011-10-01 09:38:02
7. how about if you attach microsoft word documents?p
View Tutorial By: gian at 2012-01-06 03:11:44
8. mail sent but not shown in sent items
what
View Tutorial By: Hafiz Hussaim at 2012-04-14 06:55:03
9. i cannot add attachment
its saying
&
View Tutorial By: Rintu Das at 2012-05-04 18:07:48
10. set the property of SMTP.EnableSsl = True
View Tutorial By: Kowsalya at 2012-06-03 10:47:58
11. Very nice tutorial. Hi Rintu for attachment u can
View Tutorial By: ras at 2013-03-14 17:19:47
12. Keep writing. Very helpful information provided. T
View Tutorial By: send email vb.net at 2014-08-27 09:32:03
13. Juust wish to sayy your article is as astounding.<
View Tutorial By: escorts barcelona at 2017-05-27 15:59:09