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?
 
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.

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
 
É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 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
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 = "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
 
É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 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 https://www.codeproject.com/Articles/17561/Programmatically-adding-attachments-to-emails-in-C
0
 
É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
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.