• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

How to attach a file to a mail message in vb script

I have some visual basic code that i compiled to send an email message. It works great, but I need to be able to attach a file by entering the file path as a variable when running the code:
a.exe recipient@domain.com sender@domain.com subject body attachment_path

Here is the code I have so far:

Imports System
Imports System.Web.Mail
Namespace SendMail
    Public Class usage
        Public Sub DisplayUsage()
            'Display usage instructions in case of error.
            Console.WriteLine("Usage a.exe <to> <from> <subject> <body>")
            Console.WriteLine("<to> the addresses of the email recipients")
            Console.WriteLine("<from> your email address")
            Console.WriteLine("<subject> subject of your email")
            Console.WriteLine("<body> the text of the email")
            Console.WriteLine("a.exe SomeOne@recipientdomain.com;SomeOther@recipientdomain.com Me@senderdomain.com Hi hello")
        End Sub
    End Class

    Public Class Start
        '  The main entry point for the application.
        Public Shared Sub Main(ByVal args As String())
                    Dim Message As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage
                    Message.To = args(0)
                    Message.From = args(1)
                    Message.Subject = args(2)
                    Message.Body = args(3)
                            SmtpMail.SmtpServer = "xxx"
                        Catch ehttp As System.Web.HttpException
                            Console.WriteLine("0", ehttp.Message)
                            Console.WriteLine("Here is the full error message")
                            Console.Write("0", ehttp.ToString())
                        End Try
                    Catch e As IndexOutOfRangeException
                        ' Display usage instructions if error in arguments.
                        Dim use As usage = New usage
                    End Try
                Catch ex As Exception

                End Try
            Catch e As System.Exception
                ' Display text of unknown error.
                Console.WriteLine("Unknown Exception occurred 0", e.Message)
                Console.WriteLine("Here is the Full Error Message")
                Console.WriteLine("0", e.ToString())
            End Try
        End Sub
    End Class
End Namespace
  • 2
1 Solution
If args.GetUpperBound(0) > 3 AndAlso System.IO.File.Exists(args(4)) Then
   Message.Attachments.Add(New MailAttachment(args(4)))
End If    
If the path/filename contains spaces, you need to enclose it in quotes on the command line.  (you can do the same for subject and body):

a.exe recipient@domain.com sender@domain.com "this is the subject" "this is the body" "C:\some folder\some file.txt"
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now