Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save mail form outlook with attachments?

Tags:

c#

outlook

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

like image 550
Amit Avatar asked Sep 07 '25 13:09

Amit


1 Answers

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?

like image 124
Chris Avatar answered Sep 09 '25 04:09

Chris