How to send mail using vb net code
Here is an example of how to send an email using VB.NET code:
Imports System.Net.Mail
Module SendEmail
Sub Main()
' Create a new MailMessage object
Dim mail As New MailMessage()
' Set the sender's email address
mail.From = New MailAddress("[email protected]")
' Set the recipient's email address
mail.To.Add("[email protected]")
' Set the subject of the email
mail.Subject = "Test Email"
' Set the body of the email
mail.Body = "This is a test email sent using VB.NET"
' Create a new SmtpClient object
Dim client As New SmtpClient()
' Set the SMTP server's address
client.Host = "smtp.example.com"
' Set the SMTP server's port
client.Port = 587
' Set the SMTP server's username and password
client.Credentials = New NetworkCredential("your_smtp_username", "your_smtp_password")
' Send the email
client.Send(mail)
End Sub
End Module
This code creates a new MailMessage
object and sets its properties such as the sender's email address, recipient's email address, subject, and body. It then creates a new SmtpClient
object and sets its properties such as the SMTP server's address, port, username, and password. Finally, it sends the email using the Send
method.
You can also use the MailMessage
class to send emails with attachments, HTML content, and more. Here is an example:
Imports System.Net.Mail
Module SendEmail
Sub Main()
' Create a new MailMessage object
Dim mail As New MailMessage()
' Set the sender's email address
mail.From = New MailAddress("[email protected]")
' Set the recipient's email address
mail.To.Add("[email protected]")
' Set the subject of the email
mail.Subject = "Test Email with Attachment"
' Set the body of the email
mail.Body = "This is a test email sent using VB.NET"
' Add an attachment to the email
Dim attachment As New Attachment("C:\Path\To\Attachment.txt")
mail.Attachments.Add(attachment)
' Create a new SmtpClient object
Dim client As New SmtpClient()
' Set the SMTP server's address
client.Host = "smtp.example.com"
' Set the SMTP server's port
client.Port = 587
' Set the SMTP server's username and password
client.Credentials = New NetworkCredential("your_smtp_username", "your_smtp_password")
' Send the email
client.Send(mail)
End Sub
End Module
This code adds an attachment to the email using the Attachment
class and sends the email using the SmtpClient
object.
You can also use the MailMessage
class to send emails with HTML content, using the IsBodyHtml
property:
Imports System.Net.Mail
Module SendEmail
Sub Main()
' Create a new MailMessage object
Dim mail As New MailMessage()
' Set the sender's email address
mail.From = New MailAddress("[email protected]")
' Set the recipient's email address
mail.To.Add("[email protected]")
' Set the subject of the email
mail.Subject = "Test Email with HTML Content"
' Set the body of the email as HTML
mail.IsBodyHtml = True
mail.Body = "<html><body>This is a test email sent using VB.NET</body></html>"
' Create a new SmtpClient object
Dim client As New SmtpClient()
' Set the SMTP server's address
client.Host = "smtp.example.com"
' Set the SMTP server's port
client.Port = 587
' Set the SMTP server's username and password
client.Credentials = New NetworkCredential("your_smtp_username", "your_smtp_password")
' Send the email
client.Send(mail)
End Sub
End Module
This code sets the IsBodyHtml
property to True
and sets the body of the email as HTML content using the <html>
and <body>
tags.
Note that you need to replace the placeholders ([email protected]
, [email protected]
, smtp.example.com
, your_smtp_username
, and your_smtp_password
) with your actual email address, recipient's email address, SMTP server's address, username, and password.