I'm trying to attach a file to an email that will be sent in Outlook, in C#.
I've got the following that works great:
using OutlookApp = Microsoft.Office.Interop.Outlook.Application;
[...]
public class Foo
{
private void SendMail()
{
OutlookApp outlookApp = new OutlookApp();
MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);
[...]
var rootDirectory = System.Windows.Forms.Application.StartupPath;
Attachment myAttachment = mailItem.Attachments.Add(Path.Combine(rootDirectory, @"../../Assets/image.png"));
[...]
mailItem.Display(false);
}
}
It works, but it sucks a bit because right now my file is in a static folder that won't be there when the project is built.
So I've put my image in the resources of the project: the image is now available via Properties.Resources.image
.
Naively, I've done the following:
using OutlookApp = Microsoft.Office.Interop.Outlook.Application;
[...]
public class Foo
{
private void SendMail()
{
OutlookApp outlookApp = new OutlookApp();
MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);
[...]
var rootDirectory = System.Windows.Forms.Application.StartupPath;
Attachment myAttachment = mailItem.Attachments.Add(Properties.Resources.image);
[...]
mailItem.Display(false);
}
}
But it doesn't work:
System.Runtime.InteropServices.COMException: 'Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))'
From MSDN, I read for the first parameter of the Attachments.Add
method that:
Type: System.Object
The source of the attachment. This can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.
The problem, as I understand it, is that I can only provide a file name in order to add an attachment to an Outlook mail. The other option would be to create an Outlook attachment, but if I understand it's not possible. The only way of doing that is by... Adding a file the the attachments of the mailItem
.
So my questions are the following:
Resources.resx
file in the projet? Meaning: the file path will be available in my application and the file will be "exported" when I build the project;MailItem
from a resource file properly?Thanks.
OK, in the end I adapted my method. In the build action for all the images that I needed, I did the following (this is available in the property tab for the file):
That way, at build, the folder containing my images (and my images) are copied to the build directory. I can then use their relative URL:
Attachment myAttachment = mailItem.Attachments.Add(Path.GetFullPath("Assets/image.png"));
I don't know if it's the only way to do it, but it works, it doesn't seem ugly and I can still use the images in the XAML. When images are placed in a resx
file, apparently, it's a PITA.
My guess is the resource isn't available as an item to attach. Is there a way to instead save the resource as an image to the computer somewhere and then attach that file you created?
Something like this might work.
var bmp = new Bitmap(Properties.Resources.myimage);
Attachment myAttachment = mailItem.Attachments.Add(bmp);
User contributions licensed under CC BY-SA 3.0