Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1208
  • Last Modified:

C# Outlook Late Binding add Attachment

How do I add an attachment to e-mail using assembly reflection in C#?
In other words, how do I translate:
Microsoft.Office.Interop.Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource,1, 1, "");?
The following code throws error on the attachments step.
Everything else works.
           oApp = Activator.CreateInstance(outlook);
            object mailItem = oApp.GetType().InvokeMember("CreateItem", BindingFlags.InvokeMethod, null, oApp, new object[] { 0 });
            mailItem.GetType().InvokeMember("Subject", BindingFlags.SetProperty, null,mailItem, new string[] {"Report"});
            Object attachments = mailItem.GetType().GetProperty("Attachment",BindingFlags.GetProperty);
            attachments.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, attachments, new object[] { att,1,1,"CrystalReport1.rpt.pdf" });
            mailItem.GetType().InvokeMember("Display", BindingFlags.InvokeMethod, null, mailItem, new object[] { true });
0
josephdts
Asked:
josephdts
  • 5
  • 4
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
I know it is not exactly what you are asking for but have a look at http://www.emoreau.com/Entries/Articles/2010/07/Sending-e-mails-with-attachments-a-different-method.aspx
0
 
josephdtsAuthor Commented:
Does not work for me. I would like to open Outlook with the attachment without sending it.
It seems to be easy to do it in VB.NET that is not strongly typed, but I need it on C#, and I cannot find any code on line. All my aatempts so far have failed. I got as far as getting the Attachment Object:
Object attachment = mailItem.GetType().GetProperty("Attachment", BindingFlags.SetProperty);
0
 
Éric MoreauSenior .Net ConsultantCommented:
>> I would like to open Outlook with the attachment without sending it.

this is exactly waht it is doing
>> but I need it on C#,
The downloadable demo contains a C# version!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
josephdtsAuthor Commented:
Very Interesting! I am also develping a button for the Crystal Viewer that generates e-mail with PDF attachment. I cannot find the downloadable demo  link. The code that I see uses MAPI, not Outlook.
It is more secure, but less friendly.
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
josephdtsAuthor Commented:
I have visual studio 2008. I have copied the namespace, and when I try to compile it complains that
Type or Namespace DLLImport could not be found. I cannot find it in the References either. I see it in \\Windows\System32. How do I add it as Reference? What about 64 bit?
0
 
josephdtsAuthor Commented:
Works in my test Application! Many thanks!
0
 
josephdtsAuthor Commented:
Will it work in 64 bit environment?
0
 
Éric MoreauSenior .Net ConsultantCommented:
>>Will it work in 64 bit environment?
I am on a 64-bits OS and it is working.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now