3

我正在尝试监视我的 Outlook 收件箱,因此每当新电子邮件带有附件时,我都会将附件保存到其他位置。谁能帮我吗?

4

3 回答 3

1

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

使用 ASP.NET、C# 访问 Outlook 电子邮件

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(”&lt;table style=’border:1px solid gray;font-family:Arial;font-size:x-small;width:80%;’ align=’center’&gt;<tr><td style=’width:20%;’&gt;<b>Sender :</b></td><td>”);
         str.Append(oMessage.SenderEmailAddress.ToString() + “&lt;/td></tr>”);
         //basic info about message
         str.Append(”&lt;tr><td><b>Date :</b></td><td>” + oMessage.SentOn.ToShortDateString() + “&lt;/td></tr>”);
         if (oMessage.Subject != null)
         {
             str.Append(”&lt;tr><td><b>Subject :</b></td><td>” + oMessage.Subject.ToString() + “&lt;/td></tr>”);
         }
         //reference and save all attachments

         iAttachCnt = oMessage.Attachments.Count;
         if (iAttachCnt > 0)
         {
             for (int i = 1; i <= iAttachCnt; i++)
             {
                 str.Append(”&lt;tr><td><b>Attachment(” + i.ToString() + “) :</b></td><td>” + oMessage.Attachments[i].FileName + “&lt;/td></tr>”);
             }
         }
         str.Append(”&lt;/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;

 }
于 2008-11-18T16:15:57.013 回答
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");
于 2008-11-18T16:41:41.033 回答
0

使用 Office 互操作时请注意...

而不是调用GC.Collect()你应该调用Marshal.ReleaseComObject当你完成你的包装,即 Outlook 对象。

于 2008-11-18T16:43:23.837 回答