Logo
blank Skip to main content

Reading attachments for Emails on Windows Mobile (5, 6) Devices

MDM

In my previous article, I described how to read the SMS, MMS, and Emails data from your Windows Mobile device. Now Iโ€™m going to tell you how to read the attachments from the emails. Iโ€™ll also explain where you can find the attachments to MMS messages.

 

Description of IAttach Properties

Message attachments are one or several additional “blobs” of data, such as a picture, document or sound file attached to the email by a sender. Each individual attachment to a message is supported by the IAttach interface.

Although the IAttach interface has no unique methods, it is derived from the IMAPIProp interface. Below, I describe the properties that are used to configure an attachment.

  • PR_ATTACH_DATA_BIN โ€“ itโ€™s the IStream interface, which is used to access the attachment. Property type is PT_BINARY.
  • PR_ATTACH_FILENAME โ€“ itโ€™s the display name for the attachment. The type for this property is PT_TSTRING.
  • PR_ATTACH_METHOD โ€“ this property must be set to PR_ATTACH_BY_VALUE, it has PT_LONG type.
  • PR_ATTACH_NUM โ€“ Itโ€™s the number that uniquely identifies the attachment within the message. The type for this property is PT_LONG.
  • PR_ATTACH_SIZE โ€“ itโ€™s the size, in bytes, of the attachment and all of the attachment properties. The type for this property is PT_LONG.
  • PR_ENTRYID โ€“ itโ€™s the object’s entry identifier. The type for property is PT_BINARY.
  • PR_LAST_MODIFICATION_TIME โ€“ itโ€™s the last date and time when the object was modified. The type for property is PT_SYSTIME.
  • PR_NULL โ€“ itโ€™s a NULL value. The type for property is PT_NULL.
  • PR_OBJECT_TYPE โ€“ itโ€™s the type of object. The type for property is PT_LONG.
  • PR_PARENT_ENTRYID โ€“ itโ€™s the parent object’s entry identifier. The type for property is PT_BINARY.

Working with e-mail attachments

We have already discussed how to open message, so I omit this part.

We have an already opened message. So, let’s go ahead and get the  attachments by first getting the attachment table for the message.

C
HRESULT hr = S_OK;
//check
if(pMsg ==  NULL)
{
    return E_FAIL;
}
IMAPITable *attachmentTable = NULL;
hr = pMsg->GetAttachmentTable(0, &attachmentTable);
if(FAILED(hr)) 
{ 
    return hr;
}

In this example, we query the table for the PR_ATTACH_NUM for the attachment we are interested in.

Then we need to get the properties from the table for the Attachments. For example, weโ€™ll read just three: PR_ATTACH_NUM, PR_ATTACH_SIZE, PR_ATTACH_FILENAME.

C
SRowSet *pAttachmentRowSet = NULL;
SizedSPropTagArray(3, tableAttachmentColumns) =
{3,{PR_ATTACH_NUM,PR_ATTACH_SIZE,PR_ATTACH_FILENAME}};
attachmentTable->SetColumns((LPSPropTagArray)&tableAttachmentColumns, 0);

Then, in the cycle, we can ask all attachments of the email.

C
โ€ฆ
hr = attachmentTable->QueryRows(1, 0, &pAttachmentRowSet);
if(pAttachmentRowSet->cRows != 1)
    return E_FAIL;
// Grab the properties from the table entry that you will use to open the attachment stream.
long attachmentNumber = 0;
DWORD attachmentSize = 0;
TCHAR attachmentName[MAX_PATH] = TEXT("");
attachmentNumber = pAttachmentRowSet->aRow[0].lpProps[0].Value.l;
attachmentSize = pAttachmentRowSet->aRow[0].lpProps[1].Value.ul;
_tcscpy(attachmentName, pAttachmentRowSet->aRow[0].lpProps[2].
    Value.lpszW);
FreeProws(pAttachmentRowSet);

Using the IMessage::OpenAttach() method with the attachment number, which we found in the query, we can get the IAttach interface pointer.

C
IAttach *pAttach = NULL;
hr = pMsg->OpenAttach(attachmentNumber, 0, 0, &pAttach);
if(FAILED(hr)) 
{ 
    return hr;
} 

After getting an attach interface, we can get the IStream pointer for the PR_ATTACH_DATA_BIN property.

C
LPSTREAM AttachmentStream = NULL;
TCHAR filePath[MAX_PATH] = TEXT("");
wsprintf(filePath, TEXT("%s"), attachmentName);
hr = pAttach->OpenProperty(PR_ATTACH_DATA_BIN, NULL, 0, 0,
    (IUnknown **)&AttachmentStream);

When we have the stream pointer, we can copy it to the file or vector of bytes.

C
STATSTG stg;
HRESULT hr = pstmBody->Stat(&stg,STATFLAG_NONAME);
if (FAILED(hr))
{
    return hr;
}
unsigned int bodysize = stg.cbSize.LowPart;
if(bodysize == 0)
{
    return hr;
}
unsigned char* bodybuf = new unsigned char[bodysize];
ZeroMemory(bodybuf, bodysize);
ULONG read;
hr = pstmBody->Read(bodybuf, bodysize, &read);
if (FAILED(hr) || bodysize != read)
{
    delete bodybuf; 
    return hr;
}
outBuffer.assign(bodybuf, bodybuf + bodysize);
delete [] bodybuf;

And donโ€™t forget to clean up all resources.

C
AttachmentStream->Release();
pAttach->Release();
โ€ฆ
attachmentTable->Release()

MMS attachments โ€“ short explanation

We cannot read mms attachments in the same way we read the email ones.

The MMS storage will send us an error when we ask for the properties from the attachment table.

C
IMAPITable *attachmentTable = NULL;
โ€ฆ
hr = attachmentTable->QueryRows(1, 0, &pAttachmentRowSet);
if(FAILED(hr)) 
{ 
    return hr;
}
โ€ฆ

We can find MMS attachments in the raw body of the message. My previous article can tell you about how to read the message body.

C
โ€ฆ
HRESULT hr = pMsg->OpenProperty (PR_BODY, NULL, STGM_READ, 0, (IUnknown **) &pstmBody);
โ€ฆ

The format of MMS message is SMIL (Synchronized Multimedia Integration Language). We can find the attachments looking through the raw message body.

Parsing of the SMIL body of an MMS massage is beyond the scope of this article.

Conclusion

I hope this article helped you to get to know about:

  • Work with the e-mail attachments
  • The differences between Email and MMS attachments

In this article, I didnโ€™t show how to get the MMS attachments and how to parse SMIL format.

Useful links

Download Sample Source (ZIP, 12 KB)

Read another Dev Blog article: How to USB debug Windows.

Have a question?

Ask our expert!

Tell us about your project

Send us a request for proposal! Weโ€™ll get back to you with details and estimations.

Book an Exploratory Call

Do not have any specific task for us in mind but our skills seem interesting?

Get a quick Apriorit intro to better understand our team capabilities.

Book time slot

Contact us