Menu

VB.NET TUTORIALS - VB.Net - Send Email

VB.Net - Send Email

ADVERTISEMENTS

S.NClassDescription
1AttachmentRepresents an attachment to an e-mail.
2AttachmentCollectionStores attachments to be sent as part of an e-mail message.
3MailAddressRepresents the address of an electronic mail sender or recipient.
4MailAddressCollectionStores e-mail addresses that are associated with an e-mail message.
5MailMessageRepresents an e-mail message that can be sent using the SmtpClient class.
6SmtpClientAllows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP).
7SmtpExceptionRepresents the exception that is thrown when the SmtpClient is not able to complete a Send or SendAsync operation.

ADVERTISEMENTS

The SmtpClient Class

S.NPropertyDescription
1ClientCertificatesSpecifies which certificates should be used to establish the Secure Sockets Layer (SSL) connection.
2CredentialsGets or sets the credentials used to authenticate the sender.
3EnableSslSpecifies whether the SmtpClient uses Secure Sockets Layer (SSL) to encrypt the connection.
4HostGets or sets the name or IP address of the host used for SMTP transactions.
5PortGets or sets the port used for SMTP transactions.
6TimeoutGets or sets a value that specifies the amount of time after which a synchronous Send call times out.
7UseDefaultCredentialsGets or sets a Boolean value that controls whether the DefaultCredentials are sent with requests.

ADVERTISEMENTS

S.NMethod & Description
1Dispose
Sends a QUIT message to the SMTP server, gracefully ends the TCP connection, and releases all resources used by the current instance of the SmtpClient class.
2Dispose(Boolean)
Sends a QUIT message to the SMTP server, gracefully ends the TCP connection, releases all resources used by the current instance of the SmtpClient class, and optionally disposes of the managed resources.
3OnSendCompleted
Raises the SendCompleted event.
4Send(MailMessage)
Sends the specified message to an SMTP server for delivery.
5Send(String, String, String, String)
Sends the specified e-mail message to an SMTP server for delivery. The message sender, recipients, subject, and message body are specified using String objects.
6SendAsync(MailMessage, Object)
Sends the specified e-mail message to an SMTP server for delivery. This method does not block the calling thread and allows the caller to pass an object to the method that is invoked when the operation completes.
7SendAsync(String, String, String, String, Object)
Sends an e-mail message to an SMTP server for delivery. The message sender, recipients, subject, and message body are specified using String objects. This method does not block the calling thread and allows the caller to pass an object to the method that is invoked when the operation completes.
8SendAsyncCancel
Cancels an asynchronous operation to send an e-mail message.
9SendMailAsync(MailMessage)
Sends the specified message to an SMTP server for delivery as an asynchronous operation.
10SendMailAsync(String, String, String, String)
Sends the specified message to an SMTP server for delivery as an asynchronous operation. . The message sender, recipients, subject, and message body are specified using String objects.
11ToString
Returns a string that represents the current object.

Example

Imports System.Net.Mail
Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ' Set the caption bar text of the form.   
      Me.Text = ""
   End Sub

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Try
          Dim Smtp_Server As New SmtpClient
          Dim e_mail As New MailMessage()
          Smtp_Server.UseDefaultCredentials = False
          Smtp_Server.Credentials = New Net.NetworkCredential("username@gmail.com", "password")
          Smtp_Server.Port = 587
          Smtp_Server.EnableSsl = True
          Smtp_Server.Host = "smtp.gmail.com"

          e_mail = New MailMessage()
          e_mail.From = New MailAddress(txtFrom.Text)
          e_mail.To.Add(txtTo.Text)
          e_mail.Subject = "Email Sending"
          e_mail.IsBodyHtml = False
          e_mail.Body = txtMessage.Text
          Smtp_Server.Send(e_mail)
          MsgBox("Mail Sent")

      Catch error_t As Exception
          MsgBox(error_t.ToString)
      End Try

   End Sub