How to send email from VB.NET

What is SMTP ?

SMTP (Simple Mail Transfer Protocol) is a protocol for sending e-mail messages between servers. It is an Internet standard for electronic mail (email) transmission. The default TCP port used by SMTP is 25 and the SMTP connections secured by SSL, known as SMTPS, uses the default to port 465.

SMTP Servers

email - Send e-mail via SMTP using VB.Net

SMTP provides a set of protocol that simplify the communication of email messages between email servers. Most SMTP server names are written in the form "smtp.domain.com" or "mail.domain.com": for example, a Gmail account will refer to smtp.gmail.com. Most email systems that send mail over the Internet use SMTP to send messages from one server to another, moreover the messages can then be retrieved with an email client using either POP or IMAP.

SMTP Auhentication

SMTP Authentication, often abbreviated SMTP AUTH, is an extension of the SMTP (Simple Mail Transfer Protocol) whereby an SMTP client may log in using an authentication mechanism chosen among those supported by the SMTP servers.

SmtpServer.Credentials = New _

Net.NetworkCredential("username@gmail.com", "password")

 

How do I send mail using VB.Net?

Sending email in .NET through Gmail in VB.Net

VB.NET using SMTP protocol for sending email . SMTP stands for Simple Mail Transfer Protocol. VB.NET using 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 .In the following example shows how to send an email from a Gmail address.

Send Email using Gmail in VB.Net

The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587 . Here using NetworkCredential for password based authentication.

You have to provide the informations like your gmail username and password , and the to address also.



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("username@gmail.com", "password")
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            mail = New MailMessage()
            mail.From = New MailAddress("YOURusername@gmail.com")
            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
Next Post Previous Post
No Comment
Add Comment
comment url