• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • 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("Example:")
            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())
            Try
                Try
                    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)
                    Try
                        Try
                            SmtpMail.SmtpServer = "xxx"
                            SmtpMail.Send(Message)
                        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
                        use.DisplayUsage()
                    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
0
GMJ29
Asked:
GMJ29
  • 2
1 Solution
 
PaulHewsCommented:
If args.GetUpperBound(0) > 3 AndAlso System.IO.File.Exists(args(4)) Then
   Message.Attachments.Add(New MailAttachment(args(4)))
End If    
0
 
PaulHewsCommented:
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"
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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