Solved

C# Outlook Late Binding add Attachment

Posted on 2010-08-20
9
1,153 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 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
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…

785 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