C++ Builder - Indy - Receiving certified emails

326 views Asked by At

i'm using C++ builder 6 and Indy 9/10. I'm coding to receive certified emails with attachments (basically pdf and xml files). When i receive the email, it have a TidMessageParts with one multipart/mixed part and others parts for a small text, an xml attachment with info about the certification of the email and parts for digital signature and more. My problem is: how to open the first part (multipart/mixed) to extract text and attachment in it.

See the answer i'll post.

Thank you. Fabrizio

2

There are 2 answers

6
Remy Lebeau On

TIdMessagePart is a TCollectionItem descendant. The TCollectionItem::Collection property points to the owning (parent) TCollection, not to some inner collection, like you are expecting 1. That is why mp1 and mp are pointing at the same address in memory.

You don't "open a message part" in Indy. You simply iterate the TIdMessage::MessageParts collection from one end to the other until you find the particular part you are interested in, such as by looking at their class type, ContentType properties, etc. For example:

TIdMessageParts *mp = MailMessage->MessageParts; 

for (int i = mp->Count-1; i > 0; --i)
{
    TIdMessagePart *part = mp->Items[i];

    if ((TIdAttachment *att = dynamic_cast<TIdAttachment*>(part)) != NULL)
    {
        ...
    }
    else if ((TIdText *txt = dynamic_cast<TIdText*>(part)) != NULL)
    {
        if (txt->ContentType = "text/xml")
        {
            ...
        }
        else if (txt->ContentType = "text/plain")
        {
            ...
        }
    }
}

1: support for inner nested collections has not been implemented yet, not even in Indy 10. It won't be implemented until Indy 12 at the earliest.

0
Fabrizio On

The answer to the problem i posted: @Remy told me a right observation i was unable to understand well: "The content is parsed and separated into additional collection items as needed." The email i'm reading is a 'certified email' so the real email is the attachment with .eml extension that indy where reading and i ignored (my email client shows the attachments in the contained email joined with the attachments of the base email). So, when i have right understood the Remy words and realized where to search the attachments:

  1. i have estracted the .eml file to a folder
  2. added a new TIdMessage component on the form (called PartMessage);
  3. used LoadFromFile method to read the stored file and now in PartMessage all attachments are visible and i was able to save them as files.

Thanks again to Remy as his words that where preciouses to reach the solution.

P.S. The solution works fine for Indy 9 and 10 with necessary adjustments due to different versions. P.P.S. i don't know why during my tests i found the text that the email was s/mime encoded as it is not.