Ssimple MAPI send with attachments inserts spaces

M

Mark Smith

I recently ran into a bizarre behavior with Outlook 2003 and 2007 (haven't
tested any others) and the Simple MAPI MAPISendMail function with attachments.

I'm creating a MapiMessage with only attachments in it. As long as there is
more than one attachment, for some reason the send mail dialog that pops up
has 2 spaces in the body for each attachment.

Here's the relevant code. count is the number of attachments:

MapiMessage *const pMsg = new MapiMessage;
ZeroMemory(pMsg, sizeof(MapiMessage));
pMsg->lpFiles = new MapiFileDesc[count];
ZeroMemory(pMsg->lpFiles, sizeof(MapiFileDesc) * count);

for (int i = 0; i < count; i++) {
CString filePath(...); // This is where the path to the attachment is
set - details are not relevant to this snippet
pMsg->lpFiles[pMsg->nFileCount].nPosition = -1;
pMsg->lpFiles[pMsg->nFileCount].lpszPathName = new
char[filePath.GetLength() + 1];
strcpy(pMsg->lpFiles[pMsg->nFileCount].lpszPathName, filePath);
pMsg->nFileCount++;
}

if (AfxBeginThread(sendEmailThread, pMsg) == NULL) {
deleteMsg(pMsg);
}

All the sendEmailThread does is call:
MAPISendMail(NULL, NULL, pMsg, MAPI_LOGON_UI | MAPI_DIALOG);

So the only somewhat odd thing going on here are that the MapiMessage is
created in one thread and sent on another. But I tried doing it all in the
same thread and it behaves the same.

As a test I tried setting the body to something other than NULL (I tried
both "" and "test") and in both cases I get the extra spaces plus a new line
before the specified text.

So, either this is a bug in Outlook (neither Outlook Express nor Windows
Mail do it) or I'm missing something. I looked through the flags for the
message itself and the MAPISendMail function and I didn't see anything that
seemed relevant.

The same behavior also occurs when doing a send to mail recipient in Windows
Explorer, which strongly suggests a bug in Outlook.

Any ideas?

Thanks,
Mark Smith
 
Joined
Jan 7, 2016
Messages
1
Reaction score
0
I had the same problem and found a solution. The problem is, if any file attachments have nPosition = -1, Outlook inserts a line above the message containing two spaces for each attachment where nPosition == -1. Also, if any nPosition is less than -1 or any two nPositions are equal, Outlook returns MAPI failure.

The solution is to set the nPosition of the first attachment to 0, the second to 1, and so on. This seems to get the exact outcome we would expect if they were all set to -1.

I haven't tested this with anything except Outlook 2010. Hopefully it doesn't mess up other clients.

It's worth mentioning that Windows' Send to > Mail recipient and every third party library's email functionality is also plagued by this annoyance when creating an email in Outlook.
 
Last edited:
Joined
May 8, 2022
Messages
1
Reaction score
0
I had the same problem and found a solution. The problem is, if any file attachments have nPosition = -1, Outlook inserts a line above the message containing two spaces for each attachment where nPosition == -1. Also, if any nPosition is less than -1 or any two nPositions are equal, Outlook returns MAPI failure.

The solution is to set the nPosition of the first attachment to 0, the second to 1, and so on. This seems to get the exact outcome we would expect if they were all set to -1.

I haven't tested this with anything except Outlook 2010. Hopefully it doesn't mess up other clients.

It's worth mentioning that Windows' Send to > Mail recipient and every third party library's email functionality is also plagued by this annoyance when creating an email in Outlook.

Hi, I ran into the same problem.
Your solution to assign 0, 1, 2 ... to nPosition seems to work fine.
However, I found that the values must point to a character in the body that is not a \r or \n.
So, if you have 3 attachments, and the body is "a\r\nbc\r\n", nPosition should be 0, 3 and 4.
And you must make sure that the body text is long enough.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top