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

Posted on 2005-04-22
Last Modified: 2010-05-02
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 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; 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
Question by:GMJ29
    LVL 38

    Expert Comment

    If args.GetUpperBound(0) > 3 AndAlso System.IO.File.Exists(args(4)) Then
       Message.Attachments.Add(New MailAttachment(args(4)))
    End If    
    LVL 38

    Accepted Solution

    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 "this is the subject" "this is the body" "C:\some folder\some file.txt"

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now