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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .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.
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.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

É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 MoreauSenior .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
Pat ClancyCEOAuthor 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
É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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.