Support Forums

Full Version: [TuToRiAl] Add Attachments to your Email Sender in VB
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys, Welcome to my 200th post!!! Big Grin Woot Woot Pirate!

Today you will learn how to add file attachments to your VB emailing program with multiple select. Really great feature but you must be cautious of file scanner for if it is filtered through Gmails servers such as an .exe, it will be blocked without notification.

If you want to know how to make an email sender to use attachments, then Click Here

Now to get started on features:
  • Add 1 Button
  • Add 1 OpenFileDialog
  • Add Up to 3 TextBoxes for 3 Attachments. More Textboxes, More Attachments which may result in a slow emailing. Also make the textboxes all ReadOnly = True
  • Add 3 Labels, More Labels for more attachments. Label them Attachment 1 - 3.

[Image: 339ri4j.png]

Next, the code. First off, the button.

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        file = Nothing
        OpenFileDialog1.ShowDialog()
        file = OpenFileDialog1.FileNames()
        Try
        TextBox1.Text = file(0)
        Catch ex As IndexOutOfRangeException
        EndTry
        Try
            TextBox6.Text = file(1)
        Catch ex As IndexOutOfRangeException
        End Try
        Try
            TextBox7.Text = file(2)
        Catch ex As IndexOutOfRangeException
        End Try 'All this code is for adding multiple files in one shot with Try statements.
    End Sub

Next, somewhere in the middle of the code for emailing, beginning is most preferred, paste this:

Code:
If Not TextBox1.Text = Nothing Then
                Dim attach As New Attachment(TextBox1.Text)
                MyMailMessage.Attachments.Add(attach)
            End If
            If Not TextBox2.Text = Nothing Then
                Dim attach As New Attachment(TextBox2.Text)
                MyMailMessage.Attachments.Add(attach)
            End If
            If Not TextBox3.Text = Nothing Then
                Dim attach As New Attachment(TextBox3.Text)
                MyMailMessage.Attachments.Add(attach)
            End If

This code scans for the attachments if they are there or not.

Next we need to code the OpenFileDialog1.

Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        OpenFileDialog1.DefaultExt = "C:\Users\%USERNAME%\"
        OpenFileDialog1.Title = "Select File(s) to Attach"
        OpenFileDialog1.FileName = "Select Files..."
        OpenFileDialog1.MultiSelect = True
    End Sub

Wahhhhhhhhhlllllllllllllllllllaaaaaaaaaaaaaaaaa We are done. Now select multiple files from the dialog and your good!