共用方式為


以程式設計方式從訊息中移除安全性層級 2 附件,並將其儲存至磁碟

此範例示範如何從電子郵件訊息中移除安全性層級 2 附件,並將它們儲存到磁碟,以便從中開啟。

範例

注意事項

下列程式代碼範例是 Microsoft Office Outlook 2007 程式設計應用程式的摘錄。

Outlook 可防止使用者透過具有特定擴展名的電子郵件附件傳輸惡意代碼,例如 .exe 或 .bat。 默認會封鎖這些特定附件,並識別為層級 1 附件。 層級 2 附件包含惡意代碼的機會較小,但用戶無法直接從電子郵件訊息開啟層級 2 附件。 第 2 層附件必須先儲存至磁碟。

藉由在 Attachment 物件中使用 SaveAsFile (String) 方法,您可以將附件儲存至磁碟。 在下列程式代碼範例中,RemoveAttachmentsAndSaveToDisk 會先從資料夾中的郵件專案移除所有大於指定大小的層級 2 附件。 這是藉由列舉 Attachments 集合中每個 Attachment 物件的 Type 屬性,並移除等於 olByValue 的物件來完成。 RemoveAttachmentsAndSaveToDisk 接著會使用 SaveAsFile 方法來儲存已移除的附件。

注意事項

Outlook 中的集合是線性的。 使用 Index 運算符來參考 Attachments[1] 至 Attachments[n],其中 n 代表 Count 屬性的值。

您無法使用 foreach 語句來移除集合中的專案。 請改用 Index 運算符來取得集合中的第一個專案,然後刪除專案。 然後使用 while 語句來判斷您何時刪除集合中的適當項目數目。 這可確保您已反覆查看集合中的正確項目數目。

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void RemoveAttachmentsAndSaveToDisk(string path,
    Outlook.Folder folder, int size)
{
    Outlook.Items attachItems;
    Outlook.Attachment attachment;
    Outlook.Attachments attachments;
    int byValueCount;
    int removeCount;
    bool saveMessage;
    try
    {
        // The restriction will find all items that
        // have attachments and MessageClass = IPM.Note.
        string filter = "@SQL=" + "\""
            + "urn:schemas:httpmail:hasattachment"
            + "\"" + " = True" + " AND " + "\""
            + "http://schemas.microsoft.com/mapi/proptag/0x001A001E"
            + "\"" + " = 'IPM.Note'";
        attachItems = folder.Items.Restrict(filter);
        foreach (Outlook.MailItem mail in attachItems)
        {
            saveMessage = false;
            byValueCount = 0;
            attachments = mail.Attachments;
            // Obtain the count of ByValue attachments.
            foreach (Outlook.Attachment attach in attachments)
            {
                if (attach.Size > size
                    & attach.Type ==
                    Outlook.OlAttachmentType.olByValue)
                {
                    byValueCount = byValueCount + 1;
                }
            }
            if (byValueCount > 0)
            {
                // removeCount is number of attachments to remove.
                removeCount = attachments.Count - byValueCount;
                while (mail.Attachments.Count != removeCount)
                {
                    // Use indexer to obtain 
                    // first attachment in collection.
                    attachment = mail.Attachments[1];
                    // You can refine this code to save 
                    // separate copies of attachments 
                    // with the same name.
                    attachment.SaveAsFile(path + @"\"
                        + attachment.FileName);
                    attachment.Delete();
                    if (saveMessage != true)
                    {
                        saveMessage = true;
                    }
                }
                if (saveMessage)
                {
                    mail.Save();
                }
            }
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }
}

另請參閱