Attach a file to an email

I am looking for a way to attach a file to an email in Microsoft Outlook programmatically using Visual Basic. (I'm using Visual Studio 2017) The address is a static one. I have found this code here on EE and it looks like the solution but I am unsure of how to reference Outlook.

Thank you.

       Dim olApp As Outlook.Application
        Dim olMail As Outlook.MailItem
        olApp = New Outlook.Application

        olMail = olApp.CreateItem(Outlook.OlItemType.olMailItem)
        olMail.Subject = "Your subject"
        olMail.HTMLBody = "Your HTML text"
        olMail.Attachments.Add("c:\ttt.txt", Outlook.OlAttachmentType.olByValue)

Open in new window

Pat ClancyCEOAsked:
Who is Participating?
Pat ClancyConnect With a Mentor CEOAuthor Commented:
Hi Eric,

I found this website and while it does not actually call Outlook I have gotten it to work quite well.

Thanks for your help
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
Referencing Outlook can cause issues if not all the computers are running the same version of Outlook.

If you just need to send emails, better use the System.Net.Mail namespace as shown in
Pat ClancyCEOAuthor Commented:
Thank You  Eric,

I will go through your Tutorial and get back to you.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Pat ClancyCEOAuthor Commented:
I have tried to get your solution to work and am failing at the:
 objSMTPClient.Host = "" Level.

The only thing I can think of is my SMTP requires a password.

I am kind of stumped at this point.

The company that I am creating this app has one version of outlook across its structure. I still think that calling Outlook and attaching a file to it is my best option.

Is there any way to call a right click command?  Right now the client can right click on the file they want to send then click on Send to > Mail Recipient which in turn calls up the default email program and attaches the file to it. I would like to be able to bypass this step programmatically.

Thanks you very much for your help.
Éric MoreauSenior .Net ConsultantCommented:
right-click on the file from where?
Pat ClancyCEOAuthor Commented:
Use File Explorer and go to the location where the file is located then Right Click on the file then Left Click on "Send To" in the resulting dialog then Click on Mail Recipient.

I can also use the command:

to start the default email program but I can't find a way to attach a file to the email.
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
the mailto protocol does not support attachments.

The File Explorer supports it because it was program to do it.

Have a look at
Éric MoreauSenior .Net ConsultantCommented:
your link is  using System.Net.Mail exactly as the link provided in my first comment
Pat ClancyCEOAuthor Commented:
Yes but I was not able to use a password in your solution.
Pat ClancyCEOAuthor Commented:
I had to find the solution on my own outside of EE. Eric was instrumental in helping me find a way to work around my problem.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.