Solved

C# Outlook Late Binding add Attachment

Posted on 2010-08-20
9
1,140 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
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…

919 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

14 Experts available now in Live!

Get 1:1 Help Now