Send Email with attachments from VB.NET

Use the System.Web.Mail namespace.

Create a Windows application and add the following controls:

   OpenFileDialog: ofd
   ListBox: lstAttachment 
   Button: btnRemove
   Button: btnSend
   TextBox: txtSMTPServer
   TextBox: txtFrom
   TextBox: txtTo
   TextBox: txtSubject
   TextBox: txtCC
   TextBox: txtBCC
   TextBox: txtDisplayName
   TextBox: txtMessage
   TextBox: txtSubject
   TextBox: txtReplyTo
   TextBox: txtOrganization
   CheckBox: chkFormat
   RadioButton: radHigh
   RadioButton: radNormal
   RadioButton: radLow

Add this code:

    Imports System.Web.Mail
    
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    				Handles btnAdd.Click
        Dim Counter As Integer

        ofd.CheckFileExists = True
        ofd.Title = "Select the file(s) to attach"
        ofd.ShowDialog()

        For Counter = 0 To UBound(ofd.FileNames)
            lstAttachment.Items.Add(ofd.FileNames(Counter))
        Next

    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                Handles btnRemove.Click

        If lstAttachment.SelectedIndex > -1 Then
            lstAttachment.Items.RemoveAt(lstAttachment.SelectedIndex)
        End If

    End Sub    
    
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    				Handles btnSend.Click
        Dim Counter As Integer = 0
        Dim strError As String = ""

        If txtSMTPServer.Text.Trim() = "" Then
            MessageBox.Show("Enter your SMTP server.", "Email Error", MessageBoxButtons.OK, _
            			MessageBoxIcon.Stop)
            txtSMTPServer.Focus()
            Return
        End If

        If txtFrom.Text.Trim() = "" Then
            MessageBox.Show("Enter the From address.", "Email Error", MessageBoxButtons.OK, _
            			MessageBoxIcon.Stop)
            txtFrom.Focus()
            Exit Sub
        End If

        If txtTo.Text = "" Then
            MessageBox.Show("Enter the Recipient address(es).", "Email Error", _
            			MessageBoxButtons.OK, MessageBoxIcon.Stop)
            txtTo.Focus()
            Exit Sub
        End If

        If txtSubject.Text = "" Then
            MessageBox.Show("Enter the subject.", "Email Error", MessageBoxButtons.OK, _
            			MessageBoxIcon.Stop)
            txtSubject.Focus()
            Exit Sub
        End If

        Try
            Dim objSmtpMail As System.Web.Mail.SmtpMail
            Dim Attachment As System.Web.Mail.MailAttachment
            Dim Mailmsg As New System.Web.Mail.MailMessage

            objSmtpMail.SmtpServer = txtSMTPServer.Text.Trim()

            Mailmsg.To = txtTo.Text
            Mailmsg.Cc = txtCC.Text
            Mailmsg.Bcc = txtBCC.Text
            Mailmsg.Subject = txtSubject.Text
            Mailmsg.Body = txtMessage.Text
            Mailmsg.Headers.Add("Reply-To", txtReplyTo.Text)
            Mailmsg.Headers.Add("X-Organization", txtOrganization.Text)
            Mailmsg.From = "\" & txtDisplayName.Text & "\ <" & txtFrom.Text & ">"

            If chkFormat.Checked = True Then
                Mailmsg.BodyFormat = MailFormat.Html
            Else
                Mailmsg.BodyFormat = MailFormat.Text
            End If

            If Me.radHigh.Checked Then
                Mailmsg.Priority = MailPriority.High
            ElseIf Me.radLow.Checked Then
                Mailmsg.Priority = MailPriority.Low
            Else
                Mailmsg.Priority = MailPriority.Normal
            End If

            For Counter = 0 To lstAttachment.Items.Count - 1
                Attachment = New MailAttachment(lstAttachment.Items(Counter).ToString)
                Mailmsg.Attachments.Add(Attachment)
            Next

            objSmtpMail.Send(Mailmsg)

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Email Error", MessageBoxButtons.OK, _
            			MessageBoxIcon.Stop)

        End Try

    End Sub    




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page