Search Amazon:

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    

Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Comments: 2 Items to Show:     

     
Id:  2 Posted:  7/16/2007 7:45:33 AM By:  SUDHIRAMETA
By using this code it show followin erros
(1) system.web.mail.smtpmail is absolute
(2)  objSmtpMail.Send(Mailmsg) -> error ..access of share member will not be evaluated
- -
i alos send you a problem
how to call remote exe file from another machine. in vb.NET
Id:  1 Posted:  5/19/2007 12:44:12 AM By:  Learner
hi

is there a way of displaying the email sending progress by use of a progressbar so to notify the user
that the email is being sent?

please update code or email me thanks.
 
You must log in to post feedback.
Comment:    
 

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

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms