none
MAPI: Programmatically create email with HTML body plus additional attachments RRS feed

  • Question

  • Hi there,

    I recently tried programmatically sending an Email using MAPI. The email itself should have a simple HTML body and also contain an attachment like an Excel file.

    While creating a HTML mail WITHOUT any attachments was no problem at all, it appears as if adding an attachment to a HTML mail is not such a simple task: Everytime I add an attachment, the HTML file that I have defined as the body, will also be added as an attachment so that the actual mail body will be empty.

    My approach to create a HTML mail is to create a temporary HTML file first and defining this as the first attachment. Furthermore, I will set lpszNoteText of the MapiMessage to null/nil. This way MAPI treats the attachment as the HTML formatted body of the mail - everything fine.

    Now, if I add the second attachment, the temporary HTML file will be considered as an attachment as well so that I have two attachments and the mail itself is empty.

    Is there any way to workaround this problem?

    Relevant sources:
    http://msdn.microsoft.com/en-us/library/windows/desktop/dd296732(v=vs.85).aspx
    http://www.delphigroups.info/2/5f/455363.html
    http://stackoverflow.com/questions/3366889/problem-sending-html-mails-using-mapi-and-outlook
    http://www.delphipraxis.net/1073248-post1.html

    Kind regards
    Daniel

    Monday, March 4, 2013 4:31 PM

Answers

  • There is no way to do that in Simple MAPI. The fact that Outlook's version of Simple MAPI picks up the first attachment as the HTML body is pure luck,

    You will need to use the Outlook object mode for that and explicitly set the MailItem.HTMLBody property.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    • Marked as answer by Daniel Lemke Tuesday, March 5, 2013 8:12 AM
    Monday, March 4, 2013 4:50 PM

All replies

  • There is no way to do that in Simple MAPI. The fact that Outlook's version of Simple MAPI picks up the first attachment as the HTML body is pure luck,

    You will need to use the Outlook object mode for that and explicitly set the MailItem.HTMLBody property.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    • Marked as answer by Daniel Lemke Tuesday, March 5, 2013 8:12 AM
    Monday, March 4, 2013 4:50 PM
  • Thanks for the clarification Dmitry.
    Tuesday, March 5, 2013 8:15 AM