question

PabloGlomby-5415 avatar image
0 Votes"
PabloGlomby-5415 asked

MailItem_BeforeAttachmentAdd causing Outlook issues (Array index out of bounds error)

Hi!
I am building a very simple VSTO project that runs in Outlook 2016.
This project basically registers the MailItem_BeforeAttachmentAdd event and it simply checks if the file extension is .link and in such case it reads the attachment content (that is a file path) and it attaches that file path.
So for example if you have (in your file system) a file named "myfile.link" and this file content is c:\myfile.txt then when the user attaches this myfile.link, the addin will also attach c:\myfile.txt

This is the code:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Xml.Linq;
 using Outlook = Microsoft.Office.Interop.Outlook;
 using Office = Microsoft.Office.Core;
 using System.Collections;
 using System.Runtime.InteropServices;
 using System.Windows.Forms;
 using System.Threading;
    
    
 namespace TestAttachTrap
 {
     public partial class ThisAddIn
     {
         [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
         private static extern void OutputDebugString(string message);
    
         Outlook.Inspectors m_Inspectors = null;
         ArrayList m_openedMailItems = null;
    
    
         /*********************************************************************/
         private void ThisAddIn_Startup(object sender, System.EventArgs e)
         {
             m_openedMailItems = new ArrayList();
             m_Inspectors = Application.Inspectors;
             m_Inspectors.NewInspector += Inspectors_NewInspector;
         }
    
         /*********************************************************************/
         private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
         {
         }
    
         /*********************************************************************/
         private void Inspectors_NewInspector(Outlook.Inspector Inspector)
         {
             try
             {
                 if (Inspector.CurrentItem is Microsoft.Office.Interop.Outlook.MailItem)
                 {
                     Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
                     m_openedMailItems.Add(mailItem);
                     mailItem.BeforeAttachmentAdd -= MailItem_BeforeAttachmentAdd;
                     mailItem.BeforeAttachmentAdd += MailItem_BeforeAttachmentAdd;
                 }
             }
             catch (Exception ex)
             {
                 OutputDebugString("******TestAttachTrap: Error in Inspectors_NewInspector: " + ex.ToString());
             }
         }
    
         /*********************************************************************/
         private void MailItem_BeforeAttachmentAdd(Outlook.Attachment att, ref bool bCancel)
         {
             try
             {
                 OutputDebugString("******TestAttachTrap: BeforeAttachmentAdd for: " + att.DisplayName);
                 if ( System.IO.Path.GetExtension(att.DisplayName).ToUpper()==".LINK")
                 {
                     Outlook.MailItem mailItem = (Outlook.MailItem)att.Parent;
                     string szSaveToThisPath = System.IO.Path.GetTempFileName();
                     OutputDebugString("*****TestAttachTrap: Saving to " + szSaveToThisPath);
                     if (att.GetTemporaryFilePath() != null && att.GetTemporaryFilePath().Length > 0) System.IO.File.Copy(att.GetTemporaryFilePath(), szSaveToThisPath, true);
                     else att.SaveAsFile(szSaveToThisPath);
                     string szRealPathToAttach = System.IO.File.ReadAllText(szSaveToThisPath);
                     System.IO.File.Delete(szSaveToThisPath);
                     //bCancel = true;
                     OutputDebugString("******TestAttachTrap: Redirecting to this file: "+ szRealPathToAttach);
                     mailItem.Attachments.Add(szRealPathToAttach);
                 }
             } catch( Exception ex )
             {
                 MessageBox.Show("Error adding attachment: " + ex.Message);
             }
         }
    
    
    
             #region VSTO generated code
    
             /// <summary>
             /// Required method for Designer support - do not 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
     }
 }


If you want to test, use the paper clip button to insert the .link file (else I don't know why drag&drop does not work).

The problem happens if the first file you attach produces an error. For example suppose you have a .link file pointing to a file pah that does not exist. Here the addin will launch the code that is in the exception. Then after that each time you add any file (whatever it is... a .link file, a .txt or whatever file you add) will produce an error (Array index out of bounds) for that message. If you add a .txt file the Outlook will give that error, else if it's a .link file to a valid file then the addin will give that error too.

It's like if Outlook has a kind of bad status problem...

Any idea?

Thanks in advance

office-outlook-itprooffice-addins-devoffice-vsto-com-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers