共用方式為


將附件的大小限制為 Outlook Email訊息

本主題描述如何為 Outlook 建立受控增益集,以在附件大小總計大於固定限制時取消傳送電子郵件。

提供者: Ken Getz、 MCW Technologies、LLC

指定的電子郵件訊息可以包含一或多個檔案附件,而且您可能想要限制所傳送電子郵件的附件大小總計。 本主題中的範例程式碼示範如何在 Outlook 增益集中處理 ItemSend 事件,如果所有附件的合併大小大於特定值 (2 MB,請在事件處理常式中取消電子郵件訊息的傳送,在此範例中) 。

Outlook ItemSend 事件會以其參數形式接收所傳送專案的參考,以及以傳址方式傳遞並可讓您取消傳送作業的布林值變數。 您必須在事件處理常式中自行決定是否要取消事件;如果您想要取消事件,請將Cancel 參數設定為 True 來執行此動作。

在此範例中,為了判斷總附件大小是否大於特定大小,程式碼會迴圈查看專案 Attachments 集合中的每個附件。 針對每個專案,程式碼會擷取 Size 屬性,在迴圈時加總。 如果總和超過 maxSize 常數的大小,程式碼會將變數設定 tooLargeTrue,並結束迴圈。 迴圈之後,如果 tooLarge 變數為 True,則程式碼會警示使用者,並將 Cancel 參數設定為事件處理常式 (這是以傳址方式傳遞) 為 True,導致 Outlook 取消傳送專案。

The following managed code samples are written in C# and Visual Basic. To run a .NET Framework managed code sample that needs to call into a Component Object Model (COM), you must use an interop assembly that defines and maps managed interfaces to the COM objects in the object model type library. For Outlook, you can use Visual Studio and the Outlook Primary Interop Assembly (PIA). Before you run managed code samples for Outlook 2013, ensure that you have installed the Outlook 2013 PIA and have added a reference to the Microsoft Outlook 15.0 Object Library component in Visual Studio.

You should use the following code samples in the ThisAddIn class of an Outlook add-in (using Office Developer Tools for Visual Studio). 程式碼中的Application物件必須是 所 ThisAddIn.Globals 提供的受信任 Outlook應用程式物件。 如需使用 Outlook PIA 開發受控 Outlook 解決方案的詳細資訊,請參 閱 MSDN 上的歡迎使用 Outlook 主要 Interop 元件參考

下列程式碼示範當附件大小總計大於指定的限制時,如何取消傳送電子郵件。 為了示範這項功能,請在 Visual Studio 中建立名為 LimitAttachmentSizeAddIn 的新受控 Outlook 增益集。 將 ThisAddIn.cs 或 ThisAddIn.vb 中的程式碼取代為此處顯示的範例程式碼。

using Outlook = Microsoft.Office.Interop.Outlook;
 
namespace LimitAttachmentSizeAddIn
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
          Application.ItemSend +=new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
        }
 
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }
 
        void Application_ItemSend(object Item, ref bool Cancel)
        {
            // Specify the maximum size for the attachments. For this example,
            // the maximum size is 2 MB.
            const int maxSize = 2 * 1024 * 1000;
            bool tooLarge = false;
 
            Outlook.MailItem mailItem = Item as Outlook.MailItem;
            if (mailItem != null)
            {
                var attachments = mailItem.Attachments;
                double totalSize = 0;
                foreach (Outlook.Attachment attachment in attachments)
                {
                    totalSize += attachment.Size;
                    if (totalSize > maxSize)
                    {
                        tooLarge = true;
                        break;
                    }
                }
            }
            if (tooLarge)
            {
                // If the sum of the attachment sizes is too large, alert the user
                // and cancel the send.
                System.Windows.Forms.MessageBox.Show(
                    "The total attachment size is too large. Sending canceled.", 
                    "Outlook Add-In");
                Cancel = true;
            }
        }
 
        #region VSTO generated code
 
        /// <summary>
        /// Required method for Designer support - don't modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
Public Class ThisAddIn
 
    Private Sub ThisAddIn_Startup() Handles Me.Startup
 
    End Sub
 
    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
 
    End Sub
 
    Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend
        ' Specify the maximum size for the attachments. For this example,
        ' the maximum size is 2 MB.
        Const maxSize As Integer = 2 * 1024 * 1000
        Dim tooLarge As Boolean = False
 
        Dim mailItem As Outlook.MailItem = TryCast(Item, Outlook.MailItem)
        If mailItem IsNot Nothing Then
            Dim attachments = mailItem.Attachments
            Dim totalSize As Double = 0
 
            For Each attachment As Outlook.Attachment In attachments
                totalSize += attachment.Size
                If totalSize > maxSize Then
                    tooLarge = True
                    Exit For
                End If
            Next attachment
        End If
 
        If tooLarge Then
            ' If the sum of the attachment sizes is too large, alert the user
            ' and cancel the send.
            System.Windows.Forms.MessageBox.Show(
                "The total attachment size is too large. Sending canceled.",
                "Outlook Add-In")
            Cancel = True
        End If
    End Sub
End Class

另請參閱

將檔案附加至訊息項目將 Outlook 連絡人項目附加至Email郵件修改 Outlook Email郵件的附件

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應