How to send email with attachment in VB.NET?

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


Following is the code that explains how to send email in VB.NET with attachment.

Public Sub SendMailOneAttachment(ByVal From As String, _
      ByVal sendTo As String, ByVal Subject As String, _
      ByVal Body As String, _
      Optional ByVal AttachmentFile As String = "", _
      Optional ByVal CC As String = "", _
      Optional ByVal BCC As String = "", _
      Optional ByVal SMTPServer As String = "")
 
        Dim myMessage As MailMessage
 
        Try
            myMessage = New MailMessage()
            With myMessage
                .To = sendTo
                .From = From
                .Subject = Subject
                .Body = Body
                .BodyFormat = MailFormat.Text
                'CAN USER MAILFORMAT.HTML if you prefer
 
                If CC <> "" Then .Cc = CC
                If BCC <> "" Then .Bcc = ""
 
                If FileExists(AttachmentFile) Then _
                 .Attachments.Add(AttachmentFile)
 
            End With
 
            If SMTPServer <> "" Then _
               SmtpMail.SmtpServer = SMTPServer
            SmtpMail.Send(myMessage)
 
        Catch myexp As Exception
            Throw myexp
        End Try
 
    End Sub
 
Public Sub SendMailMultipleAttachments(ByVal From As String,_
    ByVal sendTo As String, ByVal Subject As String, _
    ByVal Body As String, _
    Optional ByVal AttachmentFiles As ArrayList = Nothing, _
    Optional ByVal CC As String = "", _
    Optional ByVal BCC As String = "", _
    Optional ByVal SMTPServer As String = "")
 
        Dim myMessage As MailMessage
        Dim i, iCnt As Integer
 
        Try
            myMessage = New MailMessage()
            With myMessage
                .To = sendTo
                .From = From
                .Subject = Subject
                .Body = Body
                .BodyFormat = MailFormat.Text
                'CAN USER MAILFORMAT.HTML if you prefer
 
                If CC <> "" Then .Cc = CC
                If BCC <> "" Then .Bcc = ""
 
                If Not AttachmentFiles Is Nothing Then
                    iCnt = AttachmentFiles.Count - 1
                    For i = 0 To iCnt
                        If FileExists(AttachmentFiles(i)) Then _
                          .Attachments.Add(AttachmentFiles(i))
                    Next
 
                End If
 
            End With
 
            If SMTPServer <> "" Then _
              SmtpMail.SmtpServer = SMTPServer
            SmtpMail.Send(myMessage)
 
 
        Catch myexp As Exception
            Throw myexp
        End Try
    End Sub
 
    Private Function FileExists(ByVal FileFullPath As String) _
     As Boolean
        If Trim(FileFullPath) = "" Then Return False
 
        Dim f As New IO.FileInfo(FileFullPath)
        Return f.Exists
 
    End Function





Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.