troubleshooting Question

Adding an attachment to an outlook message in VB.Net

Avatar of pensiongenius
pensiongeniusFlag for United States of America asked on
.NET ProgrammingVisual Basic.NETMicrosoft Development
2 Comments1 Solution352 ViewsLast Modified:
In a current custom application we sometimes have automated messages that are triggered when certain events occur.  An example is a contact gets updated and the affected team members are notified in an email.  To avoid having to launch outlook and send the message we automatically send it using our exchange server here on the network.  We are at a point where we will need to send attachments (specifically excel files) stored on a network share (We already control the name and location of the file so that isn't a problem) but I need to add to the attached code to include the attachment and automate the process further.  My question is how to I add to the code to include attachments?

Shared Sub SendEmailBCC(ByVal MessageFrom As String, ByVal MessageTo As String, ByVal MessageToBCC As String, ByVal MessageSubject As String, ByVal MessageBody As String, ByVal IsHTMLFormat As Boolean)


        'Check for invalid email

        If Len(MessageFrom) = 0 Or Len(MessageTo) = 0 Then
            Exit Sub
        End If

        'Set Mail Server Here
        Dim mailServerName As String = "smtp.servernamehere.com"

        Try
            ' MailMessage is used to represent the e-mail being sent
            Using message As _
                New System.Net.Mail.MailMessage(MessageFrom, MessageTo, MessageSubject, MessageBody)
                ' Set as HTML or Not
                message.IsBodyHtml = IsHTMLFormat
                ' SmtpClient is used to send the e-mail
                Dim mailClient As New System.Net.Mail.SmtpClient(mailServerName)
                ' UseDefaultCredentials tells the mail client to use the 
                ' Windows credentials of the account (i.e. user account) 
                ' being used to run the application

                mailClient.UseDefaultCredentials = True

                ' Send delivers the message to the mail server
                mailClient.Send(message)
            End Using
            Console.WriteLine("Message sent.")
        Catch ex As FormatException
            Console.WriteLine(ex.Message)
        Catch ex As System.Net.Mail.SmtpException
            Console.WriteLine(ex.Message)
        End Try


    End Sub
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros