This project has moved and is read-only. For the latest updates, please go here.

Cannot create new Attachment and add it to MailItem.Attachments

Mar 25, 2016 at 9:29 PM
The following code crashes on mail.Attachments.Add() with error "Exception has been thrown by the target of an invocation.". The mailItem is valid, the Attachment object is valid, the path is valid, but still it crashes when I try to add it.

System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(myFilePath);
mail.Attachments.Add(attachment);

I have also tried to create a NetOffice Attachment type instead but it does not have any constructor that takes in a file path so I cannot use it. Any clue?
Outlook.Attachment a = new Outlook.Attachment();

Many thanks.
May 10, 2016 at 12:06 PM
Hi,

just came across your request. What exactly are you trying to do? I guess "mail" is a NetOffice.OutlookApi.MailItem? In this case the "attachment" should be a normal string, e.g.
Outlook.MailItem myMessage = (Outlook.MailItem)outApp.CreateItem(NetOffice.OutlookApi.Enums.OlItemType.olMailItem);
myMessage.Attachments.Add("C:\\test.txt");
May 14, 2016 at 11:25 PM
I got this solved. It was my own fault.