Problem sending modified email from MailItem.Send

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

Problem sending modified email from MailItem.Send
 
Roger Levy




Posts: 5
Joined: 2017-03-28
I am fairly new to add-ins and ADX and I'm still trying to get my first add-in to be correct. I am having a problem in my MailItem.Send event code. I append to MailItem.Body 3 different times, adding text1, hyperlink, and text2. When the event handler exits the mail is sent as I expect but a copy of the mail remains in my Outbox, with only text1 added. This copy never gets sent (it shouldn't) so I need to manually delete it. My expectation is that the copy should not be there. I release the MailItem COM object in MailItem.Close and if I open the strange mail in the Outbox I get an exception "COM object that has been separated from its underlying RCW cannot be used."
Posted 30 Apr, 2017 16:17:47 Top
Andrei Smolin


Add-in Express team


Posts: 14129
Joined: 2006-05-11
Hello Roger,

Do you cancel the send? Do you handle some other events? Try to comment out that code.

Try to do this in the ItemSend event of the Outlook Events component.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 01 May, 2017 03:37:00 Top
Roger Levy




Posts: 5
Joined: 2017-03-28
My code was making a copy in MailItem_Send() of the original mail that it only sent if the user selected an option. If the option was not selected, I did a ReleaseComObject on the mail copy and set the variable that referenced it to Nothing, however this does not seem to be enough. I changed the code so that the copy is only created if it is definitely going to be sent and that resolved the problem. Thanks.

I have a followup question. I started development of my add-in using only VSTO. I bought ADX when I learned of its compatibility across many Outlook versions. The important thing I use from ADX is the InspectorActivate event handler because I don't think a similar capability was available in VSTO until 2010 and I support 2003+. However my other events continue to be handled by VSTO event handlers. I haven't observed any problems caused by mixing the models but I am wondering if this is bad practice.
Posted 01 May, 2017 12:30:28 Top
Andrei Smolin


Add-in Express team


Posts: 14129
Joined: 2006-05-11
Hello Roger,

InspectorActivate event is part of the Outlook object model starting from Outlook 2000. VSTO add-ins and COM add-ins (Add-in Express creates COM add-ins) have a really small number of differences.

An Office add-in must be registered in the registry. In case of a COM add-in, the registration information contains the ProgID of the COM object to be created when the add-in is being loaded by Office. In case of a VSTO add-in, the registration information contains a VSTO manifest.

The information about a COM object with a specific ProgId is stored in the registry; for a per-user COM add-in, it is stored in HKCU\Software\Clasess. This means that unlike a VSTO add-in, a COM add-in can't roam across the user's profiles because HKCU\Software\Clasess doesn't roam.

Another difference is the way an add-in begins to work. When using VSTO, your add-in handles the ThisAddIn.Startup event. With an Add-in Express add-in, you handle both AddinInitialize and OnRibbonBeforeCreate events; which one occurs first depends on the host application version and the way the host application is started. The way Add-in Express loads an add-in, allows Outlook add-ins to bypass the restriction that blocks an add-in if it loads slowly; you can't do this when using VSTO.

Basically, that is all.

In other respects, the add-ins are the same: they require the developer to use the same object model of the same Office application. And this is the area where Add-in Express provides a number of components. One of them is the Outlook Events component that you CAN use instead of connecting to the corresponding events programmatically. That is, what you describe as "VSTO model" is in fact a traditional model of handling events. And yes, you can use it along with the Add-in Express components.

If you create an Add-in Express based add-in, I recommend that you use Add-in Express components to handle events. The point is these components handle the events using a set of rules; the rules are required to control how COM objects are created and released. Say, if you handle an event of an Add-in Express component and the event provides a COM object, then that COM object will be released as soon as the event is completed. If you connect to events manually, you need to do this yourself.

Please find more details in section Releasing COM objects; see https://www.add-in-express.com/docs/net-office-tips.php#releasing.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 02 May, 2017 07:17:05 Top