GetUnderlyingMessage proper usage to prevent memory leak?

Hello
Because there’s no Tnef as saveoption or when need to save MapiItemBlah as non Msg format, I’ve been given:

MapiCalendar.GetUnderlyingMessage().SaveAsTnef (… … …)

I will dispose MapiCalendar later, but what about the GetUnderlyingMessage instance?

Is the above usage correct to save as other formats?
Or need to pass the GetUnderlyingMessage to a new instance and dispose it afterwards? Like:

Using blah As MapiMessage = MyMapiCalendar.GetUnderlyingMessage

Thanks for help :slight_smile:

The MapiCalendar.GetUnderlyingMessage() method returns a MapiMessage object. If this object is not properly disposed of, it could lead to memory leaks or other resource management issues.

Using a Using Statement:

Using mapiMessage As MapiMessage = myMapiCalendar.GetUnderlyingMessage()
    mapiMessage.SaveAsTnef("path/to/save.tnef")
End Using

Manual Disposal:

Dim mapiMessage As MapiMessage = myMapiCalendar.GetUnderlyingMessage()
Try
    mapiMessage.SaveAsTnef("path/to/save.tnef")
Finally
    mapiMessage.Dispose()
End Try

The MapiCalendar instance itself should also be disposed of when you are done using it.
If both MapiCalendar and MapiMessage need to be disposed of, you can nest the Using statements or ensure both are disposed of in the correct order.