Recent

Author Topic: Sending email with Indy  (Read 4104 times)

john horst

  • Jr. Member
  • **
  • Posts: 53
    • JHorst
Re: Sending email with Indy
« Reply #30 on: April 24, 2019, 09:33:06 pm »
1)  MSG.From.Name := 'Your Name';

For my own curiosity :

Code: HTML5  [Select]
  1. <p style=3D"background-color:#627E20; font-weight: bold; color: #BAF2F2" >This is an other text.</p>

Is correct? Not working for me.
« Last Edit: April 24, 2019, 09:37:18 pm by john horst »

justnewbie

  • Full Member
  • ***
  • Posts: 225
Re: Sending email with Indy
« Reply #31 on: April 24, 2019, 09:45:02 pm »
1)  MSG.From.Name := 'Your Name';

For my own curiosity :

Code: HTML5  [Select]
  1. <p style=3D"background-color:#627E20; font-weight: bold; color: #BAF2F2" >This is an other text.</p>

Is correct? Not working for me.

Name: perfect!  :)
Html-format: yes, strange. In idHtmlPanel it doesn't work, but I looked at the email source in TB and there was this 3D addition.
Seems that idHtmlPanel cannot handle it.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 665
    • Lebeau Software
Re: Sending email with Indy
« Reply #32 on: April 24, 2019, 09:53:18 pm »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

rvk

  • Hero Member
  • *****
  • Posts: 3836
Re: Sending email with Indy
« Reply #33 on: April 24, 2019, 09:55:46 pm »
Html-format: yes, strange. In idHtmlPanel it doesn't work, but I looked at the email source in TB and there was this 3D addition. Seems that idHtmlPanel cannot handle it.
That's because idHtmlPanel just shows pure HTML. It doesn't take the printed-quotable into account. So before you show it in an HTML panel you need to convert the printed-quotable back to pure HTML.

PS. If you got incoming mail, the HTML could also be base64 encoded. Then you would also first need to decode it back to HTML before you could show it. So the encoding could be done several ways depending on the preferred choice of the sender-client. And don't get me started about Outlook/Exchange which might even give you a winmail.dat with the text inside encoded (which not many other E-mail clients besides Outlook itself can decipher).

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 665
    • Lebeau Software
Re: Sending email with Indy
« Reply #34 on: April 24, 2019, 10:04:35 pm »
1./ Currently the "From" contains my email address, but my name would be better there. Is it possible?

Yes, it is possible:

Code: Pascal  [Select]
  1. IdMessage.From.Name := 'My Name';
  2. IdMessage.From.Address := 'me@domain.com';

3./ The sending function works great with TLS and port 587 (Gmail). What should I change in the settings to get SSL and port 465 to work? I mean, there are many options in TIdSSLIOHandlerSocketOpenSSL for SSLOptions, bit confusing.

Implicit vs Explicit is a completely different thing than using SSL v2.0/3.0 vs TLS 1.0/1.1/1.2.

Port 465 uses Implicit SSL.  That means the client must initiate an SSL/TLS handshake (using SSL 2.0/3.0 or TLS 1.0/1.1/1.2 as needed) immediately upon establishing the socket connection, before then performing any STMP related I/O.  Reading the server's greeting, sending commands, reading responses, they will ALL be encrypted.  This scenario is handled by setting the TIdSMTP.Port to 465 and the TIdSMTP.UseTLS to utUseImplicitTLS, and configuring the SSLIOHandler as desired.

Port 587 uses Explicit TLS.  That means the client must NOT initiate an SSL/TLS handshake until it has first issued an unencrypted STARTTLS command and received a success reply back.  Prior to receiving a STARTTLS success response, everything will NOT be encrypted.  This scenario is handled by setting the TIdSMTP.Port to 587 and the TIdSMTP.UseTLS to utUseExplicitTLS, and configuring the SSLIOHandler as desired.

4./ Currently I'm using this method for attachment:
Code: Pascal  [Select]
  1. Attachment := TIdAttachmentFile.Create(MailMessage.MessageParts, attFile);
It only send 1 attached file. How can I send more than 1 attached files?

Simply create additional TIdAttachmentFile objects as needed, one per file.  The TIdMessage.MessageParts property is a collection, it can hold multiple TIdMessageParts-derived objects.

4. is solved with a loop:
Code: Pascal  [Select]
  1. for i := 0 to slAttachedFiles.Count - 1 do
  2.   begin
  3.     if FileExists(slAttachedFiles[i]) then TIdAttachmentFile.Create(MailMessage.MessageParts, slAttachedFiles[i]);
  4.   end;

Yes, that would be the way to go.
« Last Edit: April 24, 2019, 10:10:25 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 665
    • Lebeau Software
Re: Sending email with Indy
« Reply #35 on: April 24, 2019, 10:06:20 pm »
Your example should probably be something like this (the equal sign needs to be encoded with =3D):
<p style=3D"background-color: #627E20; font-weight: bold; color: #BAF2F2">This is an other text.</p>

Not sure if Indy does this automatically or has some functions available for it.

Yes, by default Indy encodes plain-text using the "quoted-printable" algorithm (which is what encoded '=' as '=3D'), so DO NOT encode the HTML text manually.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

rvk

  • Hero Member
  • *****
  • Posts: 3836
Re: Sending email with Indy
« Reply #36 on: April 24, 2019, 10:09:05 pm »
Yes, by default Indy encodes plain-text using the "quoted-printable" algorithm (which is what encoded '=' as '=3D'), so DO NOT encode the HTML text manually.
Then why didn't it work for justnewbie? And why did it work when doing it manually?

@justnewbie, could you show how you added the HTML-part. Maybe that's done incorrectly.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 665
    • Lebeau Software
Re: Sending email with Indy
« Reply #37 on: April 24, 2019, 10:10:05 pm »
That's because idHtmlPanel just shows pure HTML. It doesn't take the printed-quotable into account.

No, but Indy does.

So before you show it in an HTML panel you need to convert the printed-quotable back to pure HTML.

That should not be necessary, as Indy encodes with "quoted-printable" when sending an email, and decodes "quoted-printable" when receiving an email.  That way, your code can focus on just the text in its "natural" encoding (ie, in this case, the plain HTML as-is).

PS. If you got incoming mail, the HTML could also be base64 encoded.

When Indy also decodes for you.

And don't get me started about Outlook/Exchange which might even give you a winmail.dat with the text inside encoded (which not many other E-mail clients besides Outlook itself can decipher).

Indy has a standalone TIdCoderTNEF class for parsing winmail.dat attachments.
« Last Edit: April 24, 2019, 10:11:43 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 665
    • Lebeau Software
Re: Sending email with Indy
« Reply #38 on: April 24, 2019, 10:12:53 pm »
Then why didn't it work for justnewbie? And why did it work when doing it manually?

I can't answer that without seeing how exactly @justnewbie setup the TIdMessage, and what the raw email data actually looks like on the wire.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

justnewbie

  • Full Member
  • ***
  • Posts: 225
Re: Sending email with Indy
« Reply #39 on: April 24, 2019, 10:23:45 pm »
@rvk:
(@justnewbie, could you show how you added the HTML-part. Maybe that's done incorrectly.)
Based on this http://wiki.freepascal.org/TIpHtmlPanel, I made an on-the-fly stuff by using this html code:
Quote
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>This is a Heading</h1>
<p style="background-color: #A7E615;">This is a text.</p>
<p style="background-color: #627E20; font-weight: bold; color: #BAF2F2" >This is an other text.</p>

</body>
</html>
When I put the 3D, nothing changed in the idHtmlPanel.

Guys, thank you for all inputs!  :) I am out for today, tomorrow will check all of these.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 665
    • Lebeau Software
Re: Sending email with Indy
« Reply #40 on: April 25, 2019, 05:07:45 am »
Based on this http://wiki.freepascal.org/TIpHtmlPanel, I made an on-the-fly stuff by using this html code:
Quote
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>This is a Heading</h1>
<p style="background-color: #A7E615;">This is a text.</p>
<p style="background-color: #627E20; font-weight: bold; color: #BAF2F2" >This is an other text.</p>

</body>
</html>
When I put the 3D, nothing changed in the idHtmlPanel.

You are not supposed to add the '=3D' manually at all.  It should be just '=' in the actual HTML, and then Indy will encode it as '=3D' when generating the email for transmission.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

justnewbie

  • Full Member
  • ***
  • Posts: 225
Re: Sending email with Indy
« Reply #41 on: April 25, 2019, 04:51:46 pm »
Mystical problem here:
I use this in my code:
Code: Pascal  [Select]
  1. SMTP.UseTLS := utUseExplicitTLS;
But, I got this error: Error: Identifier not found "utUseExplicitTLS"
(TIdSMTP (SMTP) component dropped onto the form.)

Update: solved, uhhh. I had to add the IdExplicitTLSClientServerBase to the Uses list.
« Last Edit: April 25, 2019, 05:05:42 pm by justnewbie »

justnewbie

  • Full Member
  • ***
  • Posts: 225
Re: Sending email with Indy
« Reply #42 on: April 25, 2019, 11:54:46 pm »
Remy, I sent a message, please look at that. Thanks

justnewbie

  • Full Member
  • ***
  • Posts: 225
Re: Sending email with Indy
« Reply #43 on: April 26, 2019, 12:55:02 pm »
I experienced a few hours difference between the real sending time (my local time) and the email's date/time.
How can I solve this?

Update: I found this http://www.delphipages.com/forum/showthread.php?t=182465, but it is wrong.
None of the Time(), Date() or Now() gave proper value.
Is it a bug?
« Last Edit: April 26, 2019, 02:12:28 pm by justnewbie »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 665
    • Lebeau Software
Re: Sending email with Indy
« Reply #44 on: April 26, 2019, 08:42:00 pm »
I experienced a few hours difference between the real sending time (my local time) and the email's date/time.
How can I solve this?

Update: I found this http://www.delphipages.com/forum/showthread.php?t=182465, but it is wrong.
None of the Time(), Date() or Now() gave proper value.
Is it a bug?

The Date(), Time(), and Now() functions return the calling machine's local clock time.  The TIdMessage.Date property expects local time (also note that the TIdMessage.UseNowForDate property is True by default, unless you set the TIdMessage.Date explicitly).

TIdMessage uses Indy's LocalDateTimeToGMT() function to get the string for the email's 'Date' header.  Internally, LocalDateTimeToGMT() relies on Indy's OffsetFromUTC() function to calculate the local machine's offset from UTC time.  Did you check to make sure that OffsetFromUTC() is returning the correct offset for your local machine?

Or, are you saying that the Time(), Date() and/or Now() functions themselves are returning wrong values that do not match your local machine's actual clock?
« Last Edit: April 26, 2019, 08:45:35 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)