Solved

C# Outlook Late Binding add Attachment

Posted on 2010-08-20
9
1,134 Views
Last Modified: 2012-05-10
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
Comment
Question by:josephdts
  • 5
  • 4
9 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 33487212
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
 

Author Comment

by:josephdts
ID: 33487258
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
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 33487756
>> 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
 

Author Comment

by:josephdts
ID: 33487913
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 69

Accepted Solution

by:
Éric Moreau earned 125 total points
ID: 33487947
0
 

Author Comment

by:josephdts
ID: 33488855
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
 

Author Comment

by:josephdts
ID: 33489038
Works in my test Application! Many thanks!
0
 

Author Closing Comment

by:josephdts
ID: 33489044
Will it work in 64 bit environment?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 33490090
>>Will it work in 64 bit environment?
I am on a 64-bits OS and it is working.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now