Reading and Updating MailItem into adxOutlookAppEvents_ItemSend

Add-in Express™ Support Service
That's what is more important than anything else

Reading and Updating MailItem into adxOutlookAppEvents_ItemSend
 
franck DAMMANN




Posts: 41
Joined: 2021-01-26
Dear all,

I'm adding in my plugin a check of any attachments added in an email to be sent. to do so, I'm working with Event adxOutlookAppEvents_ItemSend and add try to add a short text into MailItem.HTMLBody, but I encounter the following error:

System.StubHelpers.StubHelpers.StubRegisterRCW(Object pThis)
   à Microsoft.Office.Interop.Outlook._MailItem.set_HTMLBody(String HTMLBody)
   à MailStoneAdd_in.AddinModule.<adxOutlookAppEvents1_ItemSend>d__86.MoveNext()


this error occurs only if I retrieve attachments from the MailItem.

Here is the code which is not working properly:

        private async void adxOutlookAppEvents1_ItemSend(object sender, ADXOlItemSendEventArgs e)
        {
           try
            {
                Outlook.MailItem mail = e.Item as Outlook.MailItem;
                Outlook.Attachments atts = mail.Attachments;
                if (atts.Count != 0)
                 {
                    for (int i = 1; i <= atts.Count; i++)
                    {
                        int sizeInBytes = atts[i].Size;
                        int sizeInKiloBytes = sizeInBytes / 1024;
                        int sizeInMbytes = sizeInKiloBytes / 1024;
                        Debug.WriteLine("Attachment: " + atts[i].FileName + ", size: " + sizeInMbytes);
                    }
                 }
               mail.HTMLBody = "ok"; <----- GENERATES THE EXCEPTION
               Debug.WriteLine("To: " + mail.To + " CC: " + mail.CC + " BCC: " + mail.BCC); <---- Even this one generated an exception


on the other hand, If I'm doing the same thing but without retrieving the attachments, there is no error and HTMLBody is correctly accessed:

        private async void adxOutlookAppEvents1_ItemSend(object sender, ADXOlItemSendEventArgs e)
        {
           try
            {
                Outlook.MailItem mail = e.Item as Outlook.MailItem;
                mail.HTMLBody = "ok"; <----- WORKING FINE
                Debug.WriteLine("To: " + mail.To + " CC: " + mail.CC + " BCC: " + mail.BCC); <---- WORKING FINE


May be the retrieval of Attachments is releasing the mail object ? or do I need to add something ?

Thanks for your help.

Franck.
Posted 24 Aug, 2022 04:30:16 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hello Franck,

Try releasing COM objects created in your code. The Attachments collection is a COM object; every item in that collection is a COM object; every call atts[i] creates a new COM object. You need to get release all of them. Check if doing this before setting HTMLBody helps.

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 24 Aug, 2022 07:18:21 Top
franck DAMMANN




Posts: 41
Joined: 2021-01-26
Thanks Andrei,

I tried to Release atts before accessing to the mail object but same result, it seems I lose the access to the mail Item as soon as I access to its attachments...
Posted 24 Aug, 2022 10:20:01 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
franck DAMMANN writes:
it seems I lose the access to the mail Item


I suppose you've released the mail variable.

Does this issue occur if you create a new add-in project with that event handler only?

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 25 Aug, 2022 03:12:55 Top
franck DAMMANN




Posts: 41
Joined: 2021-01-26
I'll try and let you know.

Thanks.
Posted 25 Aug, 2022 03:35:07 Top
franck DAMMANN




Posts: 41
Joined: 2021-01-26
Andrei,

I think I understood...
in my loop to parse attachments, I'm performing an Async Task to send information via POST to a backend, and this operation makes me lose the context...and I cannot access to the ADXOlItemSendEventArgs neither mail anymore, even if I try to declare a new var after the post, it generates an error:



COM object that has been separated from its underlying RCW cannot be used


I tried to comment the POST command and it works fine...any idea why I lose the context after sending this POST command ?
Posted 25 Aug, 2022 03:54:13 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hello Franck,

Check [URL=https://www.add-in-express.com/forum/read.php?FID=5&TID=15848]Add-in Express.com: [forum] Async/await with COM plugins[/URL].

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 25 Aug, 2022 04:47:21 Top
franck DAMMANN




Posts: 41
Joined: 2021-01-26
ok I see, I'll change my code correspondingly.

Thanks !
Posted 25 Aug, 2022 08:59:34 Top