vb.net allows you to send email from your application. The system.net.mail namespace contains classes used to send email to a Simple Mail Transfer Protocol (SMTP) server for delivery.
The following table lists some of these commonly used classes −
attached file
represents a file attached to an email.
collection of attachments
stores attachments to be sent as part of an email message.
email address
represents the address of an email sender or recipient.
collection of email addresses
stores email addresses associated with an email message.
mail message
represents an email message that can be sent using the smtpclient class.
smtp client
allows applications to send email using the simple mail transfer protocol (smtp).
smtpe exception
represents the exception that is thrown when smtpclient is unable to complete a send or sendasync operation.
the smtpclient class
The smtpclient class allows applications to send email using the Simple Mail Transfer Protocol (smtp).
the following are some of the commonly used properties of the smtpclient class −
client certificates
specifies which certificates should be used to establish the secure sockets layer (ssl) connection.
credentials
gets or sets the credentials used to authenticate the sender.
enable ssl
specifies whether smtpclient uses secure sockets layer (ssl) to encrypt the connection.
host
gets or sets the name or ip address of the host used for smtp transactions.
port
gets or sets the port used for smtp transactions.
waiting time
gets or sets a value that specifies the amount of time after which a synchronous send call times out.
use default credentials
gets or sets a boolean that controls whether default credentials are sent with requests.
the following are some commonly used methods of the smtpclient class −
delete
sends an outgoing message to the smtp server, gracefully ends the tcp connection, and releases all resources used by the current instance of the smtpclient class.
delete (boolean)
sends an outgoing message to the smtp server, gracefully ends the tcp connection, releases all resources used by the current instance of the smtpclient class, and optionally removes managed resources.
send completed
raises the submit completed event.
send(mail message)
sends the specified message to an smtp server for delivery.
send(string, string, string, string)
sends the specified email message to an smtp server for delivery. the message sender, recipients, subject, and message body are specified using string objects.
sendasync(mail message, object)
sends the specified email 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.
sendasync(string, string, string, string, object)
sends an email 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.
send sync
cancels an asynchronous operation to send an email message.
sendmailasync(mail message)
sends the specified message to an smtp server for delivery as an asynchronous operation.
sendmailasync(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.
tostring
returns a string representing the current object.
The following example demonstrates how to send mail using the smtpclient class. the following points should be noted in this regard −
-
you must specify the host smtp server you use to send email. the host and port properties will be different for different host servers. we will use the gmail server.
You must provide the credentials for authentication, if required by the smtp server.
You must also provide the sender’s email address and the recipient’s email address(es) using the mailmessage.from and mailmessage.to properties, respectively . .
You must also specify the content of the message using the mailmessage.body property.
example
In this example, we are going to create a simple application that sends an email. follow these steps −
-
add three labels, three text boxes and a button control to the form.
change the text properties of the tags to – ‘from’, ‘to:’ and ‘message:’ respectively.
change the name properties of the texts to txtfrom, txtto and txtmessage respectively.
change the button control’s text property to ‘submit’
add the following code in the code editor.
-
You must provide your real gmail address and password for credentials.
when the above code is executed and run with the launch button available in the microsoft visual studio toolbar, the following window will be displayed, which you will use to send your emails, try it for yourself same.
-
-