Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Idmessagehelper::TIdMessageHelper_SaveToFile issue?


This question is answered.


Permlink Replies: 4 - Last Post: Nov 28, 2016 3:32 PM Last Post By: John May
John May

Posts: 81
Registered: 6/25/10
Idmessagehelper::TIdMessageHelper_SaveToFile issue?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 28, 2016 4:41 AM
boost::scoped_ptr<TIdMessage>            IdMsg(new TIdMessage(this));
boost::scoped_ptr<TIdMessageBuilderHtml> IdMsgBldrHtml(new TIdMessageBuilderHtml);
 
IdMsg->Subject                          = "This is example subject";
IdMsg->From->Name                       = "This is (example) name";
IdMsg->From->Address                    = "this@example.addr";
 
TIdEMailAddressItem* ai                 = IdMsg->Recipients->Add();
ai->Name                                = "This is \\";
ai->Address                             = "this@another.addr";
 
IdMsgBldrHtml->PlainTextCharSet         = "utf-8";
IdMsgBldrHtml->HtmlCharSet              = "utf-8";
IdMsgBldrHtml->HtmlViewerNeededMsg      = "HTML viewer is required to see this message.";
 
IdMsgBldrHtml->PlainText->Text = "This is example text";
IdMsgBldrHtml->Html->Text      = "<body>This is example <b>HTML</b></body>";
IdMsgBldrHtml->FillMessage(IdMsg.get());
 
#pragma warn -8111
Idmessagehelper::TIdMessageHelper_SaveToFile(IdMsg.get(), "testmsg.eml", false, false);
#pragma warn .8111


The resulting EML file is:

From: "This is (example) name" <this@example.addr>
Subject: This is example subject
To: "This is \\" <this@another.addr>
Content-Type: multipart/alternative; boundary="XeWIUaW4kfPKqCVIv2Ogqg1=_jdeZ3Ddxk"
MIME-Version: 1.0
Date: Mon, 28 Nov 2016 13:36:03 +0100
 
This is a multi-part message in MIME format
 
--XeWIUaW4kfPKqCVIv2Ogqg1=_jdeZ3Ddxk
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
 
This is example text
 
--XeWIUaW4kfPKqCVIv2Ogqg1=_jdeZ3Ddxk
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
 
<body>This is example <b>HTML</b></body>
 
--XeWIUaW4kfPKqCVIv2Ogqg1=_jdeZ3Ddxk--


The backslash in To is doubled.

Should be:
To: "This is \" <this@another.addr>


I use currently version 5373. Looks like an issue with the helper function as IdMsg Recipients seems fine after FillMessage and before SaveToFile helper, as then there is only single \ (shown in debugger as single char \ \ ). This forum also has a problem if I write two consequent backslashes one after another - then it is dropped.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Idmessagehelper::TIdMessageHelper_SaveToFile issue? [Edit]
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 28, 2016 11:35 AM   in response to: John May in response to: John May
John wrote:

The backslash in To is doubled.

That has nothing to do with TIdMessageHelper, that is simply how TIdEmailAddressItem
encodes email addresses in general. The following logic is in the EncodeAddressItem()
function in IdCoderHeader.pas:

for I := 1 to Length(EmailAddr.Name) do
begin              { quote special characters }
  if (EmailAddr.Name[I] = '\') or (EmailAddr.Name[I] = '"') then begin
    S := S + '\';    {Do not Localize}
  end;
  S := S + EmailAddr.Name[I];
end;


Per RFC 5322 (and earlier RFCs), the "\" character is reserved and must be
escaped. The "To" value you are specifying falls under the "name-addr" definition
of "mailbox", which does not allow unescaped "\" characters in the name portion.

Should be:

To: "This is \" <this@another.addr>

No, it should not. That is malformed, as it is an unclosed quoted string
due to escaping the inner quote character.

Looks like an issue with the helper function

It is not an issue with the helper itself. It is just how encoding works
in general.

IdMsg Recipients seems fine after FillMessage and before SaveToFile helper

Because the email is not encoded until it is saved.

as then there is only single \ (shown in debugger as single char \ \ ).

In C/C++, "\" is an escape character in a string literal. That is not the
case in Delphi.

--
Remy Lebeau (TeamB)
John May

Posts: 81
Registered: 6/25/10
Re: Idmessagehelper::TIdMessageHelper_SaveToFile issue?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 28, 2016 2:26 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
No, it should not. That is malformed, as it is an unclosed quoted string
due to escaping the inner quote character.

OK, but if the backslash should be quoted when saving why is it not unquoted when loading?

boost::scoped_ptr<TIdMessage>			 IdMsg(new TIdMessage(this));
 
#pragma warn -8111
Idmessagehelper::TIdMessageHelper_LoadFromFile(IdMsg.get(), "testmsg.eml", false, false);
#pragma warn .8111
 
Application->MessageBox(IdMsg->Recipients->Items[0]->Name.c_str(), L"Result", MB_OK);


The previously saved message above To name loads as:

This is \\


Isn't it supposed to be then the following?

This is \


And just to be clear - the above two examples are how text appears as text as message box shows it - nothing related to C++ duplicate backslashes which represent a single backslash in debugger view. LoadFromFile appears not to unquote the quoted backslash here but now when you mentioned it - it doesn't unquote the quote character either:

I also tried with loading of quoted quote char and it is also not unquoted:

To: "This is \" \\" <this@another.addr>
 
loads as:
 
This is \" \\


The email programs load the same as:

This is " \
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Idmessagehelper::TIdMessageHelper_SaveToFile issue?
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 28, 2016 2:56 PM   in response to: John May in response to: John May
John May wrote:
OK, but if the backslash should be quoted when saving why is it not unquoted when loading?

A logic bug in the TIdEMailAddressItem.Text property setter that is skipping escaped characters between quotes. This is actually a known problem that has not been fixed yet:

TIdEmailAddress fails for valid addresses
https://indy.codeplex.com/workitem/15341

I have added your examples to the ticket.

The previously saved message above To name loads as:

This is \\


Isn't it supposed to be then the following?

This is \

Yes.

--
Remy Lebeau (TeamB)
John May

Posts: 81
Registered: 6/25/10
Re: Idmessagehelper::TIdMessageHelper_SaveToFile issue?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 28, 2016 3:32 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thank you for the reply and adding examples to issue!
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02