How to mail from vb net
In VB.NET, you can use the System.Net.Mail
namespace to send emails. Here's a step-by-step guide on how to do it:
Step 1: Add the necessary references
In your VB.NET project, add the following references:
System.Net.Mail
System.Net
Step 2: Create a new MailMessage
object
Create a new instance of the MailMessage
class, which represents an email message:
Imports System.Net.Mail
Dim mail As New MailMessage()
Step 3: Set the sender's email address and name
Set the sender's email address and name using the From
property:
mail.From = New MailAddress("[email protected]", "Your Name")
Step 4: Set the recipient's email address and name
Set the recipient's email address and name using the To
property:
mail.To.Add(New MailAddress("[email protected]", "Recipient Name"))
Step 5: Set the subject and body of the email
Set the subject and body of the email using the Subject
and Body
properties:
mail.Subject = "Test Email from VB.NET"
mail.Body = "This is a test email sent from VB.NET"
Step 6: Set the SMTP server settings
Set the SMTP server settings using the SmtpClient
class:
Dim smtp As New SmtpClient()
smtp.Host = "your_smtp_server_name"
smtp.Port = 587
smtp.Credentials = New NetworkCredential("your_smtp_username", "your_smtp_password")
Step 7: Send the email
Use the Send
method of the SmtpClient
class to send the email:
smtp.Send(mail)
Here's the complete code:
Imports System.Net.Mail
Module SendEmail
Sub Main()
Dim mail As New MailMessage()
mail.From = New MailAddress("[email protected]", "Your Name")
mail.To.Add(New MailAddress("[email protected]", "Recipient Name"))
mail.Subject = "Test Email from VB.NET"
mail.Body = "This is a test email sent from VB.NET"
Dim smtp As New SmtpClient()
smtp.Host = "your_smtp_server_name"
smtp.Port = 587
smtp.Credentials = New NetworkCredential("your_smtp_username", "your_smtp_password")
smtp.Send(mail)
End Sub
End Module
Make sure to replace the placeholders ([email protected]
, your_smtp_server_name
, your_smtp_username
, and your_smtp_password
) with your actual email address, SMTP server name, username, and password.
That's it! With these steps, you should be able to send an email from your VB.NET application.