Solved

VB.net System.Net.Mail add attachment

Posted on 2014-04-09
2
526 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 26

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

20 Experts available now in Live!

Get 1:1 Help Now