Send an E-Mail using VB.NET
Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
6 posts
Page 1 of 1
Hello guys.
Welcome to my new tutorial.
In this tutorial I'll be teaching you how to send an E-Mail using VB.NET. I decided to choose this topic as a tutorial because I made an E-Mail sender for yesterday's Mad March challenge lol.
OK so lets start!
First up open Visual Studio and create a new project. Name it anything that you want.
Next when the form loads make a GUI similar to this.
Name your From TextBox as txtFrom, Server TextBox as txtServer, Username TextBox as txtUsername, Password TextBox as txtPassword, To TextBox as txtTo, Subject TextBox as txtSubject, Message RichTextBox as rtbmessage, ... button as btnBrowse, Send Button as btnsend, Include HTML CheckBox as CheckBox1 and Include Attachment CheckBox as CheckBox2.
Next Right Click on the Form --> View Code --> Add this code under Public Form1.
Declares new OpenfileDialog. Shows the OpenFileDialog and if the user clicks the OK button, displays the path of the file selected in txtAttachment.
What the code under the btnSend click event does -
Declares from as a New MailAddress that is your from address in the txtFrom textbox.
Declares toaddress as a New MailAddress that is your to address in the txtTo textbox.
Declares mail1 as a new MailMessage and the (from, toaddress) is the from address and the To address.
Declares att and assigns it the value of txtAttachment.Text
Checks if the chkHTML is checked or not.
If it is checked then declares the body of the message to be in the HTML format.
Then sets the mail1 message body as the rtbmessage.Text.
Sets the mail1 subject as txtSubject.Text.
If the chkHTML is not checked then does the same but does not declare the body of the message to be in HTML format.
Declares tehclient as a New SMTPClient which is txtServer.Text.
Declares tehcredentials as New System.Net.NetworkCredential which is your username(txtUsername.Text) and your password (txtPassword.Text)
Sets the credentials of tehclient as tehcredentials.
Enables SSL
Sends the message!
And that's it you have created and understood how to create an E-Mail Sender!
Hope you liked it!
Regards,
Skillful
Welcome to my new tutorial.
In this tutorial I'll be teaching you how to send an E-Mail using VB.NET. I decided to choose this topic as a tutorial because I made an E-Mail sender for yesterday's Mad March challenge lol.
OK so lets start!
First up open Visual Studio and create a new project. Name it anything that you want.
Next when the form loads make a GUI similar to this.
Name your From TextBox as txtFrom, Server TextBox as txtServer, Username TextBox as txtUsername, Password TextBox as txtPassword, To TextBox as txtTo, Subject TextBox as txtSubject, Message RichTextBox as rtbmessage, ... button as btnBrowse, Send Button as btnsend, Include HTML CheckBox as CheckBox1 and Include Attachment CheckBox as CheckBox2.
Next Right Click on the Form --> View Code --> Add this code under Public Form1.
Code: Select all
Now you will get a few errors. To fix those just add this at the top of Public Class Form1.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
Dim ofd As New OpenFileDialog
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
txtattachment.Text = ofd.FileName
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Dim from As New MailAddress(txtFrom.Text)
Dim toaddress As New MailAddress(txtTo.Text)
Dim mail1 As New MailMessage(from, toaddress)
Dim att = txtattachment.Text
If chkHTML.Checked = True Then
mail1.IsBodyHtml = True
mail1.Body = rtbmessage.Text
mail1.Subject = txtSubject.Text
If chkAttachment.Checked Then
mail1.Attachments.Add(New Attachment(att))
End If
Dim tehclient As New SmtpClient(txtServer.Text)
Dim tehcredentials As New System.Net.NetworkCredential(txtUsername.Text, txtPassword.Text)
tehclient.Credentials = tehcredentials
tehclient.EnableSsl = True
Try
tehclient.Send(mail1)
MessageBox.Show("Message sent successfully!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("There was some problem sending your message. Please try again.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Else
mail1.IsBodyHtml = False
mail1.Body = rtbmessage.Text
mail1.Subject = txtSubject.Text
If chkAttachment.Checked Then
mail1.Attachments.Add(New Attachment(att))
End If
Dim tehclient As New SmtpClient(txtServer.Text)
Dim tehcredentials As New System.Net.NetworkCredential(txtUsername.Text, txtPassword.Text)
tehclient.Credentials = tehcredentials
tehclient.EnableSsl = True
Try
tehclient.Send(mail1)
MessageBox.Show("Message sent successfully!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex2 As Exception
MessageBox.Show("There was some problem sending your message. Please try again.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAttachment.CheckedChanged
If chkAttachment.Checked Then
btnBrowse.Enabled = True
txtattachment.Enabled = True
Else
btnBrowse.Enabled = False
txtattachment.Enabled = False
End If
End Sub
Code: Select all
So now the Final Code should look something like this -
Imports System.Net.Mail
Code: Select all
What the code under the btnBrowse click event does - Imports System.Net.Mail
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
Dim ofd As New OpenFileDialog
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
txtattachment.Text = ofd.FileName
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Dim from As New MailAddress(txtFrom.Text)
Dim toaddress As New MailAddress(txtTo.Text)
Dim mail1 As New MailMessage(from, toaddress)
Dim att = txtattachment.Text
If chkHTML.Checked = True Then
mail1.IsBodyHtml = True
mail1.Body = rtbmessage.Text
mail1.Subject = txtSubject.Text
If chkAttachment.Checked Then
mail1.Attachments.Add(New Attachment(att))
End If
Dim tehclient As New SmtpClient(txtServer.Text)
Dim tehcredentials As New System.Net.NetworkCredential(txtUsername.Text, txtPassword.Text)
tehclient.Credentials = tehcredentials
tehclient.EnableSsl = True
Try
tehclient.Send(mail1)
MessageBox.Show("Message sent successfully!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("There was some problem sending your message. Please try again.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Else
mail1.IsBodyHtml = False
mail1.Body = rtbmessage.Text
mail1.Subject = txtSubject.Text
If chkAttachment.Checked Then
mail1.Attachments.Add(New Attachment(att))
End If
Dim tehclient As New SmtpClient(txtServer.Text)
Dim tehcredentials As New System.Net.NetworkCredential(txtUsername.Text, txtPassword.Text)
tehclient.Credentials = tehcredentials
tehclient.EnableSsl = True
Try
tehclient.Send(mail1)
MessageBox.Show("Message sent successfully!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex2 As Exception
MessageBox.Show("There was some problem sending your message. Please try again.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAttachment.CheckedChanged
If chkAttachment.Checked Then
btnBrowse.Enabled = True
txtattachment.Enabled = True
Else
btnBrowse.Enabled = False
txtattachment.Enabled = False
End If
End Sub
End Class
Declares new OpenfileDialog. Shows the OpenFileDialog and if the user clicks the OK button, displays the path of the file selected in txtAttachment.
What the code under the btnSend click event does -
Declares from as a New MailAddress that is your from address in the txtFrom textbox.
Declares toaddress as a New MailAddress that is your to address in the txtTo textbox.
Declares mail1 as a new MailMessage and the (from, toaddress) is the from address and the To address.
Declares att and assigns it the value of txtAttachment.Text
Checks if the chkHTML is checked or not.
If it is checked then declares the body of the message to be in the HTML format.
Then sets the mail1 message body as the rtbmessage.Text.
Sets the mail1 subject as txtSubject.Text.
If the chkHTML is not checked then does the same but does not declare the body of the message to be in HTML format.
Declares tehclient as a New SMTPClient which is txtServer.Text.
Declares tehcredentials as New System.Net.NetworkCredential which is your username(txtUsername.Text) and your password (txtPassword.Text)
Sets the credentials of tehclient as tehcredentials.
Enables SSL
Sends the message!
And that's it you have created and understood how to create an E-Mail Sender!
Hope you liked it!
Regards,
Skillful
Instead of LOL use this -
LSIBMHBIWFETALOL
Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
LSIBMHBIWFETALOL
Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
To make multi attachments:
Add listview to form. Set it's view to details.
Add 'Browse' or 'Add' button and inside put this :
Now, to add attachments to mail use this:
Add listview to form. Set it's view to details.
Add 'Browse' or 'Add' button and inside put this :
Code: Select all
I placed it like that so you see your file name without it's full location.
Dim opfd as new openfiledialog
opfd.showdialog
if opfd.filename ="" then exit sub
dim lvi as new listviewitem
lvi.text = opfd.safefilename
lvi.tag = opfd.filename
listview.items.add(lvi)
Now, to add attachments to mail use this:
Code: Select all
For each lvi as listviewitem in listview.items
Dim att as new net.attachment(lvi.tag)
'Dim mail1 As New MailMessage(from, toaddress)
'This is already declared like in code above by skillful
'So mail1 is mailmessage
mail1.attachments.add(att)
Next
'Send mail
Thanks for the updates M1z23R. cooll;
I'll update the post when I get home.
I'll update the post when I get home.
Instead of LOL use this -
LSIBMHBIWFETALOL
Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
LSIBMHBIWFETALOL
Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
Thanks. Ok I can send e-mail,but how to receive e-mail? :P
but how to receive e-mail?You would need to implement an SMTP server to directly receive emails, or if the messages are stored elsewhere you could access them with a POP3/IMAP client.
6 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023