views:

430

answers:

3
+3  Q: 

Saving Attachment

I am trying to monitor my outlook inbox so whenever new emails come in with attachments I save the attachment to some other location. Can anyone help me out?

+1  A: 

This is not a complete solution, but it describes some of the fundamental tools you'll be using in the Outlook API.

From Access Outlook Emails with ASP.NET, C#:

using Outlook;

 Outlook.Application oOutlook;
 Outlook.NameSpace oNs;
 Outlook.MAPIFolder oFldr;
 long iAttachCnt;

 try
 {
     oOutlook = new Outlook.Application();
     oNs = oOutlook.GetNamespace(”MAPI”);

     //getting mail folder from inbox
     oFldr = oNs.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
     Response.Write(”Total Mail(s) in Inbox :” + oFldr.Items.Count + “<br>”);
     Response.Write(”Total Unread items = ” + oFldr.UnReadItemCount);
     foreach (Outlook.MailItem oMessage in oFldr.Items)
     {
         StringBuilder str = new StringBuilder();
         str.Append(”<table style=’border:1px solid gray;font-family:Arial;font-size:x-small;width:80%;’ align=’center’><tr><td style=’width:20%;’><b>Sender :</b></td><td>”);
         str.Append(oMessage.SenderEmailAddress.ToString() + “</td></tr>”);
         //basic info about message
         str.Append(”<tr><td><b>Date :</b></td><td>” + oMessage.SentOn.ToShortDateString() + “</td></tr>”);
         if (oMessage.Subject != null)
         {
             str.Append(”<tr><td><b>Subject :</b></td><td>” + oMessage.Subject.ToString() + “</td></tr>”);
         }
         //reference and save all attachments

         iAttachCnt = oMessage.Attachments.Count;
         if (iAttachCnt > 0)
         {
             for (int i = 1; i <= iAttachCnt; i++)
             {
                 str.Append(”<tr><td><b>Attachment(” + i.ToString() + “) :</b></td><td>” + oMessage.Attachments[i].FileName + “</td></tr>”);
             }
         }
         str.Append(”</table><br>”);
         Response.Write(str.ToString());

     }

 }
 catch (System.Exception ex)
 {
     Response.Write(”Execption generated:” + ex.Message);
 }
 finally
 {
     GC.Collect();
     oFldr = null;
     oNs = null;
     oOutlook = null;

 }
Adam Davis
A: 

Outlook Redemption is the best thing currently to use that I have found. It will allow you to get into the messages and extract the attachments and the message bodies. I am using it now to do just that. It also keeps the security dialogs from appearing when you access the messages.

Here is some code I use in a class. I included the constructor and the processing function I use to save off the attachments. I cut out the code that is specific to my needs but you can get an idea of what to use here.

    private RDOSession _MailSession = new RDOSession();
    private RDOFolder _IncommingInbox;
    private RDOFolder _ArchiveFolder;
    private string _SaveAttachmentPath;

    public MailBox(string Logon_Profile, string IncommingMailPath, 
                   string ArchiveMailPath, string SaveAttPath)
    {
        _MailSession.Logon(Logon_Profile, null, null, true, null, null);
        _IncommingInbox = _MailSession.GetFolderFromPath(IncommingMailPath);
        _ArchiveFolder = _MailSession.GetFolderFromPath(ArchiveMailPath);
        _SaveAttachmentPath = SaveAttPath;
    }
public void ProcessMail()
        {

            foreach (RDOMail msg in _IncommingInbox.Items)
            {
                foreach (RDOAttachment attachment in msg.Attachments)
                {
                    attachment.SaveAsFile(_SaveAttachmentPath + attachment.FileName);
                    }
                }
                if (msg.Body != null)
                {
                    ProcessBody(msg.Body);
                }

            }

        }

This is how I call it and what is passed

MailBox pwaMail = new MailBox("Self Email User", @"\\Mailbox - Someone\Inbox",
                              @"\\EMail - Incomming\Backup", @"\\SomePath");
StubbornMule
A: 

Just a word of caution when using Office Interop...

Instead of calling GC.Collect() you should call Marshal.ReleaseComObject when you are done with your wrapper i.e. the Outlook object.

RandomNoob
You might give a little more insight as to why this suggestion should be followed.
Adam Davis
Maybe you should click the hyperlink? Pasting verbose content from MSDN doesn't seem helpful.
RandomNoob