保存附件

时间:2008-11-18 16:08:34

标签: c# .net outlook attachment

我正在尝试监控我的Outlook收件箱,因此每当新电子邮件附带附件时,我都会将附件保存到其他位置。任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

这不是一个完整的解决方案,但它描述了您将在Outlook API中使用的一些基本工具。

来自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;

 }

答案 1 :(得分:0)

Outlook Redemption是我发现的最好用的东西。它将允许您进入消息并提取附件和消息正文。我现在正在使用它来做到这一点。它还可以防止在您访问消息时出现安全对话框。

这是我在课堂上使用的一些代码。我包含了构造函数和我用来保存附件的处理函数。我删除了特定于我需要的代码,但是你可以知道在这里使用什么。

    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);
                }

            }

        }

这就是我所说的以及传递的内容

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

答案 2 :(得分:0)

使用Office Interop时要小心谨慎......

当您使用包装器(即Outlook对象)时,应该调用GC.Collect(),而不是调用Marshal.ReleaseComObject