Solved

C# Outlook Late Binding add Attachment

Posted on 2010-08-20
9
1,196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 70

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 70

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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
 
LVL 70

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 70

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
This collection of functions covers all the normal rounding methods of just about any numeric value.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Suggested Courses

627 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