Solved

VB.net System.Net.Mail add attachment

Posted on 2014-04-09
2
529 Views
Last Modified: 2014-04-10
Hi
I am using the following code to send a message from my VB.net Windows forms app
How do I add attachment?
Public Sub SendMail(ByVal MsgFrom As String, ByVal MsgTo As String, ByVal MsgSubject As String, ByVal MsgBody As String)

        Try

            '  Pass in the message information to a new MailMessage

            Dim msg As New Net.Mail.MailMessage(MsgFrom, MsgTo, MsgSubject, MsgBody)



            '   Create an SmtpClient to send the e-mail

            Dim mailClient As New SmtpClient("105.236.251.36")  '  = local machine IP Address


            '  Use the Windows credentials of the current User

            mailClient.UseDefaultCredentials = True



            ' Pass the message to the mail server

            mailClient.Send(msg)

            '  Optional user reassurance:

            'MsgBox(String.Format("Message Subject ' {0} ' successfully sent From {1} To {2}", MsgSubject, MsgFrom, MsgTo), "EMail", Windows.Forms.MessageBoxButtons.OK)

            '  Housekeeping

            msg.Dispose()

        Catch ex As FormatException

            MsgBox(ex.Message & " :Format Exception")

        Catch ex As SmtpException

            MsgBox(ex.Message & " :SMTP Exception")

        End Try
    End Sub

Open in new window

0
Comment
Question by:murbro
2 Comments
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 39990794
Add this line after the "Dim msg As New Net.Mail.MailMessage(MsgFrom, MsgTo, MsgSubject, MsgBody)" line
msg.Attachments.Add(New Attachment(File, "C:\Test\test.txt"))

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 39991005
Thanks
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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