Question

I'd like my program to compose an outgoing e-mail in the user's favorite e-mail client by passing a MapiMessage to MAPISendMail. Elements of the e-mail, such as the subject, are available to my program as immutable strings (e.g. const char*). Is it considered "safe" to use const_cast< LPSTR > to pass the addresses of these strings directly to the MAPI implementation?

I speculate that LPSTR is used in MAPI only because the API is very old, and years ago there was either not widespread practice of using immutable strings, or there was some arcane reason why they needed to be mutable. On the other hand, perhaps there is a good reason why the strings need to be mutable, and I just don't see it. Unfortunately, I've not been able to find much information to support this, one way or the other.

Was it helpful?

Solution

I do not think it is safe. From MAPISEndMail documentation "If no information is missing, the message can be sent as is or the user can be prompted to verify the information and change values if necessary. Both MAPISendMailW and MAPISendMail differ from the MAPISendDocuments function in that they allow greater flexibility in message generation. Message text Some client applications can truncate subject lines that are too long or contain carriage returns, line feeds, or form feeds."

This to me indicates that the members of MapiMessage may be altered. Also, MAPI cannot be more ancient than Win32 SDK, which contains plenty of LPCSTR.

I would just allocate new buffers and copy your const strings there before calling MAPISendMail

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top