Can't get rid of winmail.dat, I'm pulling my hair out

Add-in Express™ Support Service
That's what is more important than anything else

Can't get rid of winmail.dat, I'm pulling my hair out
 
Steve Smith


Guest


Hello!

I've spent hours searching on the Internet and on the add-in-express forums. But the solution outlined in this thread just doesn't seem to work:

http://www.add-in-express.com/forum/read.php?PAGEN_1=4&FID=5&TID=4057&phrase_id=1659293#nav_start

The problem:

I'm adding a UserProperty to new e-mails which my add-in uses for certain purposes. If attachments are added to the e-mail along with the UserProperty it's sent in the proprietary TNEF format - which means other e-mail clients (apart from Outlook) can't read it or can only see a winmail.dat file.

According to the page linked above, I've tried deleting the UserProperty in the OnItemSend event. But the e-mail is still sent in TNEF format.

I've also tried setting UseTNEF to false inside the OnItemSend event and even somewhere else in my code.

OutlookSpy shows that UseTNEF is false when I set it, but once the message appears in the Sent Items folder, UseTNEF is automatically set to true! The UserProperties are successfully deleted though, but this is not making me happier...

Any ideas on how to fix this issue?

Here is my code:

OnItemSend event:

var
  MailItem: _MailItem;
begin
  Item.QueryInterface(IID__MailItem, MailItem);
  if MailItem <> nil then
  begin
    try
        while MailItem.UserProperties.Count > 0 do
          MailItem.UserProperties.Remove(1);

        MailItem.Save;
    finally
      MailItem := nil;
    end;
  end;
end;


And I'm setting UseTNEF to false with the following code:

function SetUseTNEFViaMAPI(const MailItem: MailItem; UseTnef: Boolean): Boolean;
const
  UseTnef_GUID: TGUID = '{00062008-0000-0000-C000-000000000046}';
  PT_BOOLEAN = ULONG(11);
var
  MAPIMail: IMAPIProp;
  namProp: Array[0..0] of TMAPINAMEID;
  Names: Array[0..0] of PMAPINAMEID;
  namTag: PSPropTagArray;
  pProp: TSPropValue;
  PR_PROPERTY: ULONG;
begin
  Result := false;

  if Assigned(HrSetOneProp) then
  begin
    MAPIMail := IMessage(MailItem.MAPIObject);
    try
      with namProp[0] do
      begin
        lpguid := @UseTnef_GUID;
        ulKind := MNID_ID;
        Kind.lID := $8582;
      end;

      Names[0] := @namProp[0];

      if MAPIMail.GetIDsFromNames(1, Names[0], MAPI_CREATE, namTag) = S_OK then
      try
        PR_PROPERTY := namTag^.aulPropTag[0] or PT_BOOLEAN;
        FillChar(pProp, SizeOf(TSPropValue), 0);
        pProp.ulPropTag := PR_PROPERTY;
        pProp.Value.b := SmallInt(UseTnef);

        if HrSetOneProp(Imessage(MAPIMail), @pProp) = S_OK then
          Result := true;
      finally
        MAPIFreeBuffer(namTag);
      end;
    finally
      MAPIMail := nil;
    end;
  end;
end;


Thank you!
Posted 24 Jan, 2011 08:02:55 Top
Dmitry Kostochko


Add-in Express team


Posts: 2875
Joined: 2004-04-05
Hi Steve,

As far as I remember, one of the reasons why winmail.dat may get attached to an email is sending the original email in Microsoft Outlook Rich Text Format instead of the Plain Text or HTML formats. In which format (HTML, RTF or Plain Text) do you send your messages?
Posted 24 Jan, 2011 09:11:50 Top
Steve Smith


Guest


Hello Dmitry,

In Plain Text format.
Posted 24 Jan, 2011 10:23:45 Top
Dmitry Kostochko


Add-in Express team


Posts: 2875
Joined: 2004-04-05
Hi Steve,

Thank you for the additional information.

Could you please send your project (or even better some small demo project that shows the issue) to me for testing? What Outlook version and what non-MS email client should I use for testing?
Posted 24 Jan, 2011 12:40:45 Top
Steve Smith


Guest


Thank you Dmitry,

I've sent a sample project to the support e-mail address.

I've tested it with Outlook2000 / Outlook2007 and Outlook Express as the receiver to produce the error.

Please let me know if you need anything else.
Posted 25 Jan, 2011 07:38:14 Top
Dmitry Kostochko


Add-in Express team


Posts: 2875
Joined: 2004-04-05
Hi Steve,

Thank you for the code sample and detailed description.
I have just reproduced the issue using my Outlook 2010 x86 as the sender email client and The Bat! as the receiver. I will try to find a solution and let you know about my results soon.
Posted 25 Jan, 2011 09:47:47 Top
Steve Smith


Guest


Thanks to Dmitry the problem is now solved. The solution is to call MAPI SaveChanges:

Instead of

if HrSetOneProp(Imessage(MAPIMail), @pProp) = S_OK then
Result := true;

You need to call:

if HrSetOneProp(Imessage(MAPIMail), @pProp) = S_OK then begin
MAPIMail.SaveChanges(KEEP_OPEN_READWRITE);
Result := true;
end;

And you need to change OnItemSend to:

try
while MailItem.UserProperties.Count > 0 do
MailItem.UserProperties.Remove(1);

// CHANGED
MailItem.Save;

SetUseTNEFViaMAPI(MailItem, false);
finally
MailItem := nil;
end;

Hope it helps someone else too!
Posted 25 Jan, 2011 12:45:40 Top