May 13, 2021 vb.net
Vb. N et allows e-mail to be sent from your application. The System.Net.Mail namespace contains classes that are used to send e-mail messages to the Simple Mail Transport Protocol (SMTP) server for delivery.
The following table lists some commonly used classes:
SN | Class | Describe |
---|---|---|
1 |
Attachment
|
Represents an attachment to an e-mail message. |
2 | AttachmentCollection | Stores attachments to be sent as part of an e-mail message. |
3 |
MailAddress
|
Represents the address of the sender or recipient of the e-mail message. |
4 | MailAddressCollection | Store the e-mail address associated with the e-mail message. |
5 |
MailMessage
|
Indicates an e-mail message that can be sent using the SmtpClient class. |
6 | SmtpClient | Allows applications to send e-mail messages using a simple messaging protocol (SMTP). |
7 | SmtpException | Represents an exception thrown when SmtpClient cannot complete a send or SendAsync operation. |
The SmtpClient class allows applications to send e-mail messages using a simple message transport protocol (SMTP).
Here are some common properties of the SmtpClient class:
SN | Property | Describe |
---|---|---|
1 | ClientCertificates | Specify which certificates should be used to establish a secure socket layer (SSL) connection. |
2 |
Credentials
|
Gets or sets up credentials to verify the sender. |
3 | EnableSsl | Specify whether SmtpClient uses a secure socket layer (SSL) to encrypt the connection. |
4 |
Host
|
Gets or sets the name or IP address of the host used for SMTP transactions. |
5 |
Port
|
Gets or sets the port for SMTP transactions. |
6 |
Timeout
|
Gets or sets a value that specifies the amount of time it takes for the synchronous send call to time out. |
7 | UseDefaultCredentials | Gets or sets a Boolean value that controls whether Default Credits is sent with the request. |
Here are some common methods for the SmtpClient class:
SN | Methods and instructions |
---|---|
1 |
Dispose Send a QUAT message to the SMTP server, normally end the TCP connection, and free all resources used by the current instance of the SmtpClient class. |
2 |
Dispose(Boolean) Send a QUAT message to the SMTP server, normally end the TCP connection, free all resources used by the current instance of the SmtpClient class, and optionally dispose of managed resources. |
3 |
OnSendCompleted The SendCompleted event was raised. |
4 |
Send(MailMessage) Send the specified message to the SMTP server for delivery. |
5 |
Send(String,String,String,String) Send the specified e-mail message to the SMTP server for delivery. T he message sender, recipient, subject, and message body are specified using the String object. |
6 |
SendAsync(MailMessage,Object) Send the specified e-mail message to the SMTP server for delivery. T his method does not block the calling thread and allows the caller to pass an object to the method called when the operation is complete. |
7 |
SendAsync(String,String,String,String,Object) Send e-mail to the SMTP server for delivery. T he message sender, recipient, subject, and message body are specified using the String object. This method does not block the calling thread and allows the caller to pass an object to the method called when the operation is complete.
|
8 |
SendAsyncCancel Cancel the asynchronous operation to send an e-mail message. |
9 |
SendMailAsync(MAILMESSAGE) Send the specified message to deliver the SMTP server as an asynchronous operation. |
10 |
SendMailAsync(MailMessage) Send the specified message to the SMTP server for delivery as an asynchronous operation.
|
11 |
Tostring Returns a string that represents the current object.
|
The following example shows how to send a message using the SmtpClient class. I n this regard, attention should be paid to the following:
You must specify the SMTP host server for sending e-mail messages. H ost and port properties will vary from host server to host server. W e will use the gmail server.
If the SMTP server is required, you need to grant authentication credentials.
You should also use the MailMessage.from and MailMessage.To properties to provide the sender's email address and the recipient's email address, respectively.
You should also specify the message content using the MailMessage.Body property.
In this example, let's create a simple application that sends e-mail messages. F ollow these steps:
Add three labels, three text boxes, and a button control to the form.
Change the text properties of the label to - "From," "To:" and "Message:", respectively.
Change the name properties of the text to txtFrom, txtTo, and txtMessage, respectively.
Change the text property of the button control to Send
Add the following code to the code editor.
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 = "tutorialspoint.com" 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("[email protected]", "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
You must provide your gmail address and real password to obtain credentials.
When you execute and run the code above using the Start button on the Microsoft Visual Studio toolbar, the following window is displayed, which you will use to send e-mail and try it out for yourself.