Reputation: 26266
I wanted to save Outlook mails in to msg format along with the attachment through C#.
I tried the following code
using Outlook = Microsoft.Office.Interop.Outlook;
private void button1_Click(object sender, EventArgs e)
{
Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
foreach (Outlook.MailItem item in inbox.Items)
{
item.SaveAs(finename, Outlook.OlSaveAsType.olMSG);
}
}
It could save the mail as msg but the attachment part was removed. SaveAs method had no other overloads alos... :(
If i try to save a message from outlook it saves the message along with the attachment embedded in it. Any idea how this can be achieved..?
I am using .Net Framework 3.5 and Outolook 2007
Upvotes: 4
Views: 14154
Reputation: 31
What are you using as a filename? does it end with .msg?
I do something like this and it works as you describe you want it too:
Outlook.MailItem msg;
foreach (object obj in f.Mapi.Items)
{
try
{
msg = obj as Outlook.MailItem;
// ... set file name using message attributes
// string fullPath = "something" + ".msg"
msg.SaveAs(fullPath, Outlook.OlSaveAsType.olMSG);
}
}
The reason I'm so curious in your case is that I am wondering how I can reproduce what you are doing: saving the mail item with out saving the attachments?
Upvotes: 3
Reputation: 1273
I believe that you will have to save them separately.
Use the Attachments property on the MailItem to get all the attachments. then loop through them and call SaveAsFile() for each of the attachments.
examples in below link are for basic, but it should work in C# as well
MailItem::Attachments http://msdn.microsoft.com/en-us/library/bb207129.aspx
Attachment::SaveAsFile http://msdn.microsoft.com/en-us/library/bb219926.aspx
Upvotes: 0