1

I have created a form using Winform to send email. My Outlook 2010 Window is opening and is working fine. However, on my friend machine, it is not working because he has Outlook 2013. Is it possible to create a send mail which will work on both Outlook 2010 and 2013. Below is the code I am using:

 var outlookApp = new Outlook.Application();

 Outlook.MailItem mailItem = outlookApp.CreateItem(Outlook.OlItemType.olMailItem);

 mailItem.Subject = this.txtTitle.Text;
 mailItem.HTMLBody = this.HtmlText;
 mailItem.HTMLBody = mailItem.HTMLBody + ReadSignature();
 mailItem.Importance = Outlook.OlImportance.olImportanceLow;
 mailItem.Display(true);

Moreover, the COM I have use is Microsoft Outlook 14.0 Object and I know for Outlook 2013 it is Microsoft Outlook 15.0 Object

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Hishaam Namooya
  • 1,071
  • 1
  • 11
  • 22
  • You can [send an email](http://stackoverflow.com/a/32767496/3110834) in html format and containing some images. What's the requirement which moves you toward using outlook interop to send email? – Reza Aghaei Aug 29 '16 at 14:43
  • The reason is because from the winform I have to set the images and title based on user input from the form directly. Also, I need to send the mail through Outlook since I need to use the company email address and so on. – Hishaam Namooya Aug 29 '16 at 14:46
  • All of these can be done using [`SmtpClient`](https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx) without any problem. Take a look at the [link](http://stackoverflow.com/a/32767496/3110834) which I shared in first comment or [this one](http://stackoverflow.com/a/36926380/3110834). – Reza Aghaei Aug 29 '16 at 14:51
  • Ok. I will give it a try – Hishaam Namooya Aug 29 '16 at 14:55
  • @RezaAghaei I have check with my superior and they say they require it to be opened in Outlook since they may modified the Email before sending it and also the Email on the Winform acts only as a template – Hishaam Namooya Aug 29 '16 at 15:10
  • After you added a reference to `Microsoft.Office.Interop.Outlook.dll`. Right click on the dll reference and set `Embed Interop Types` to `true`. – Reza Aghaei Aug 29 '16 at 15:19
  • Ok. Can you please explain to me the purpose of setting it to true? By default it is already set to True – Hishaam Namooya Aug 29 '16 at 15:20
  • Let me know if you have any question about the answer or if you find it useful :) – Reza Aghaei Aug 30 '16 at 11:58

1 Answers1

1

If you want to have version independence, after you added a reference to the desired office interop assemly, for example Microsoft.Office.Interop.Outlook.dll, right click on the dll reference and choose properties and in property grid, set Embed Interop Types to true.

For more information:

Walkthrough: Embedding Type Information from Microsoft Office Assemblies in Visual Studio

If you embed type information in an application that references COM objects, you can eliminate the need for a primary interop assembly (PIA). Additionally, the embedded type information enables you to achieve version independence for your application. That is, your program can be written to use types from multiple versions of a COM library without requiring a specific PIA for each version. This is a common scenario for applications that use objects from Microsoft Office libraries. Embedding type information enables the same build of a program to work with different versions of Microsoft Office on different computers without the need to redeploy either the program or the PIA for each version of Microsoft Office.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • By default the Embed Interop Type is set to True and still it did not worked. I had to install the Outlook 2013 and it is now working. Once the implementation is completed, I will check if it is backward compatible. – Hishaam Namooya Aug 30 '16 at 12:00
  • 1
    Additional to [MSDN](https://msdn.microsoft.com/en-us/library/mt632251.aspx) link which I shared, also take a look at this article, you will find it useful [Painless Office Interop Using Visual C#](http://www.claudiobernasconi.ch/2014/02/13/painless-office-interop-using-visual-c-sharp/) – Reza Aghaei Aug 30 '16 at 12:04