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.Recipients.Add("bla.bla@bla.com")
        olMail.Subject = "Your subject"
        olMail.HTMLBody = "Your HTML text"
        olMail.Attachments.Add("c:\ttt.txt", Outlook.OlAttachmentType.olByValue)
        olMail.Send() 

Open in new window

Pat ClancyCEOAsked:
Who is Participating?

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

x
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 http://www.emoreau.com/Entries/Articles/2007/09/Using-SystemNetMail.aspx
1
Pat ClancyCEOAuthor Commented:
Thank You  Eric,

I will go through your Tutorial and get back to you.
0
Pat ClancyCEOAuthor Commented:
I have tried to get your solution to work and am failing at the:
 objSMTPClient.Host = "my.mailhost.ca" 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.
0
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?
0
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:
System.Diagnostics.Process.Start("mailto:someone@somewhere.ca")

to start the default email program but I can't find a way to attach a file to the email.
0
É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 https://www.codeproject.com/Articles/17561/Programmatically-adding-attachments-to-emails-in-C
0
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.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/2bc834d0-8f95-417c-89e0-7f7694be3e53/send-email-with-attachment-to-default-mail-client?forum=vbgeneral

Thanks for your help
Pat
0

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
0
Pat ClancyCEOAuthor Commented:
Yes but I was not able to use a password in your solution.
0
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.
0
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.