Recent

Author Topic: Synapse SMTPSend Subject Field  (Read 9473 times)

Jacob21

  • New Member
  • *
  • Posts: 13
Synapse SMTPSend Subject Field
« on: April 26, 2017, 09:09:47 pm »
Could anyone indicate how I can set the Subject field of an EMail sent using Synapse SMTPSend (All other fields set ok) Thanks

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1311
    • Lebeau Software
Re: Synapse SMTPSend Subject Field
« Reply #1 on: April 26, 2017, 10:50:00 pm »
Could anyone indicate how I can set the Subject field of an EMail sent using Synapse SMTPSend (All other fields set ok) Thanks

The SendTo() and SendToEx() methods of TSMTPSend have a Subject parameter:

Code: [Select]
var
  Email: TStringList;
begin
  Email := TStringList.Create;
  try
    Email.Add('Hello World');
    SMTP.SendTo('me@me.com', 'you@you.com', 'Test Subject', 'smtp.server.com', Email);
  finally
    Email.Free;
  end;
end;

Are you saying that parameter does not work?

If you use the SendToRaw() method instead, its MailData parameter contains the complete raw data for the email, including headers:

Code: [Select]
var
  Email: TStringList;
begin
  Email := TStringList.Create;
  try
    Email.Add('From: "Me" <me@me.com>');
    Email.Add('To: "You" <you@you.com>');
    Email.Add('Subject: Test Subject'); // <--
    Email.Add('');
    Email.Add('Hello World');
    SMTP.SendToRaw('me@me.com', 'you@you.com', 'smtp.server.com', Email, 'username', 'password');
  finally
    Email.Free;
  end;
end;

In this case, you can use TMimeMess to create the raw email data.  It has a Subject property:

Code: [Select]
var
  Text: TStringList;
  Email: TMimeMess;
begin
  Email := TMimeMess.Create;
  try
    Email.Header.From := '"Me" <me@me.com>';
    Email.Header.ToList.Add('"You" <you@you.com>');
    Email.Header.Subject := 'Test Subject'; // <--

    Text := TStringList,Create;
    try
      Text.Add('Hello World');
      Email.AddPartText(Text, nil);
    finally
      Text.Free;
    end;

    Email.EncodeMessage;
    SMTP.SendToRaw('me@me.com', 'you@you.com', 'smtp.server.com', Email.Lines, 'username', 'password');
  finally
    Email.Free;
  end;
end;

Read Synapse's documentation for more details:

http://synapse.ararat.cz/doc/help/smtpsend.html
« Last Edit: April 26, 2017, 10:52:34 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Jacob21

  • New Member
  • *
  • Posts: 13
Re: Synapse SMTPSend Subject Field
« Reply #2 on: April 27, 2017, 01:36:15 am »
Thank you Remy,
I think I must have some kind of unit interface problem.
My SMTPSend unit does not provide SendTo (or SendToEx) functions??
My code has:
uses: smtpsend,  mimemess, etc
var  SMTP: TSMTPSend;
 begin    SMTP:=TSMTPSend.Create;
I can SMTP.MailTo(MailTo) or  SMTP.MailData(m.lines);
but SMTP.SendTo gives fatal error
Compile Project, Target: MikeBlue.exe: Exit code 1, Errors: 1
"BlueU1.pas(98,14) Error: identifier idents no member "SendTo"
I'm wondering why I'm only getting a subset of the documented SMTPSend functions. I'm using Synapse Release 40 2012-04-23 from Synapse40.zip
Strange.

Jacob21

  • New Member
  • *
  • Posts: 13
Re: Synapse SMTPSend Subject Field
« Reply #3 on: April 28, 2017, 01:01:52 pm »
Deleted Synapse,
Loaded Indy10
Now working 100%

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1311
    • Lebeau Software
Re: Synapse SMTPSend Subject Field
« Reply #4 on: April 28, 2017, 08:45:31 pm »
My SMTPSend unit does not provide SendTo (or SendToEx) functions??

My bad.  The documentation did not make this clear, but the SendTo...() functions are not members of the TSMTPSend class itself, they are standalone functions instead (that use TSMTPSend internally).  I confirmed this by looking at the SMTPSend.pas source file.  So, my earlier examples were wrong, they should be more like this instead:

Code: [Select]
var
  Email: TStringList;
begin
  Email := TStringList.Create;
  try
    Email.Add('Hello World');
    smtpsend.SendTo('me@me.com', 'you@you.com', 'Test Subject', 'smtp.server.com', Email);
  finally
    Email.Free;
  end;
end;

Code: [Select]
var
  Email: TStringList;
begin
  Email := TStringList.Create;
  try
    Email.Add('From: "Me" <me@me.com>');
    Email.Add('To: "You" <you@you.com>');
    Email.Add('Subject: Test Subject'); // <--
    Email.Add('');
    Email.Add('Hello World');
    smtpsend.SendToRaw('me@me.com', 'you@you.com', 'smtp.server.com', Email, 'username', 'password');
  finally
    Email.Free;
  end;
end;

Code: [Select]
var
  Text: TStringList;
  Email: TMimeMess;
begin
  Email := TMimeMess.Create;
  try
    Email.Header.From := '"Me" <me@me.com>';
    Email.Header.ToList.Add('"You" <you@you.com>');
    Email.Header.Subject := 'Test Subject'; // <--

    Text := TStringList,Create;
    try
      Text.Add('Hello World');
      Email.AddPartText(Text, nil);
    finally
      Text.Free;
    end;

    Email.EncodeMessage;

    smtpsend.SMTP.SendToRaw('me@me.com', 'you@you.com', 'smtp.server.com', Email.Lines, 'username', 'password');
  finally
    Email.Free;
  end;
end;

Otherwise, if you want to use TSMTPSend yourself, it would look more like this:

Code: [Select]
var
  SMTP: TSMTPSend;
  Email: TStringList;
begin
  Email := TStringList.Create;
  try
    Email.Add('From: "Me" <me@me.com>');
    Email.Add('To: "You" <you@you.com>');
    Email.Add('Subject: Test Subject'); // <--
    Email.Add('');
    Email.Add('Hello World');

    SMTP := TSMTPSend.Create;
    try
      SMTP.TargetHost := 'smtp.server.com';
      SMTP.Username := 'username';
      SMTP.Password := 'password';
      if SMTP.Login then
      begin
        if SMTP.MailFrom('me@me.com', Length(Email.Text)) then
        begin
          if SMTP.MailTo('you@you.com') then
            SMTP.MailData(Email);
        end;
        SMTP.Logout;
      end;
    finally
      SMTP.Free;
    end;
  finally
    Email.Free;
  end;
end;

Or:

Code: [Select]
var
  SMTP: TSMTPSend;
  Text: TStringList;
  Email: TMimeMess;
begin
  Email := TMimeMess.Create;
  try
    Email.Header.From := '"Me" <me@me.com>';
    Email.Header.ToList.Add('"You" <you@you.com>');
    Email.Header.Subject := 'Test Subject'; // <--

    Text := TStringList,Create;
    try
      Text.Add('Hello World');
      Email.AddPartText(Text, nil);
    finally
      Text.Free;
    end;

    Email.EncodeMessage;

    SMTP := TSMTPSend.Create;
    try
      SMTP.TargetHost := 'smtp.server.com';
      SMTP.Username := 'username';
      SMTP.Password := 'password';
      if SMTP.Login then
      begin
        if SMTP.MailFrom('me@me.com', Length(Email.Lines.Text)) then
        begin
          if SMTP.MailTo('you@you.com') then
            SMTP.MailData(Email.Lines);
        end;
        SMTP.Logout;
      end;
    finally
      SMTP.Free;
    end;
  finally
    Email.Free;
  end;
end;
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

egsuh

  • Hero Member
  • *****
  • Posts: 1266
Re: Synapse SMTPSend Subject Field
« Reply #5 on: February 01, 2022, 09:56:10 am »
I know this thread is quite long ago. Still I add my question here.

In my case, only smtpsend.SendtoEX works fine (not 100% fine, but anyway mail is sent).
smtpSend.SendToRaw, and using TSMTPSend directly --> Mail is sent, but the recipients do not accept it.

smtpSend.SendTo :  sending mail fails (Result is false).

Does anybody have similar experience?

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1311
    • Lebeau Software
Re: Synapse SMTPSend Subject Field
« Reply #6 on: February 01, 2022, 10:19:44 pm »
smtpSend.SendToRaw, and using TSMTPSend directly --> Mail is sent, but the recipients do not accept it.

Meaning what, exactly?  If the mail is sent without error, then the SMTP portion is working.  Are the recipients rejecting the email once it reaches their inboxes? How do you know exactly?  Are you getting errors in your sender's inbox?

smtpSend.SendTo :  sending mail fails (Result is false).

Hard to say why without seeing the actual SMTP command/response traffic going back and forth.  Do you have a trace of the raw traffic?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

egsuh

  • Hero Member
  • *****
  • Posts: 1266
Re: Synapse SMTPSend Subject Field
« Reply #7 on: February 02, 2022, 06:14:52 am »
Hi Remy,
SendToRaw works fine, just like SendToEx.

But I've tested following codes, which displays up to "OK", but still there are no mails received. If I run startTLS, tihs does not seem to work in my mail server. Mail failes.

Code: Pascal  [Select][+][-]
  1. procedure MailSend(const sSmtpHost, sSmtpPort, sSmtpUser, sSmtpPasswd,
  2.                        sFrom, sTo, Content: String);
  3. var
  4.   smtp: TSMTPSend;
  5.   msg_lines: TStringList;
  6.  
  7.   procedure AddtoLog(s:string);
  8.   begin
  9.      if Assigned(OutStrings) then OutStrings.Append(s);
  10.   end;
  11.  
  12. begin
  13.   msg_lines := TStringList.Create;
  14.   smtp := TSMTPSend.Create;
  15.   try
  16.       msg_lines.Text := Content;
  17.  
  18.       smtp.UserName := sSmtpUser;
  19.       smtp.Password := sSmtpPasswd;
  20.  
  21.       smtp.TargetHost := sSmtpHost;
  22.       smtp.TargetPort := sSmtpPort;
  23.  
  24.       SMTP.AutoTLS:= false; // true;
  25.       SMTP.FullSSL:=Trim(sSMTPPort)<>'25';  // if sending to port 25, don't use encryption
  26.  
  27.       AddToLog('SMTP Login');
  28.       if not smtp.Login() then  raise ESMTP.Create('SMTP ERROR: Login:' + smtp.EnhCodeString);
  29.       {
  30.       AddToLog('SMTP StartTLS');
  31.       if not smtp.StartTLS() then raise ESMTP.Create('SMTP ERROR: StartTLS:' + smtp.EnhCodeString); }
  32.  
  33.       AddToLog('SMTP Mail');
  34.       if not smtp.MailFrom(sFrom, Length(sFrom)) then  raise ESMTP.Create('SMTP ERROR: MailFrom:' + smtp.EnhCodeString);
  35.       if not smtp.MailTo(sTo) then  raise ESMTP.Create('SMTP ERROR: MailTo:' + smtp.EnhCodeString);
  36.       if not smtp.MailData(msg_lines) then  raise ESMTP.Create('SMTP ERROR: MailData:' + smtp.EnhCodeString);
  37.  
  38.        AddToLog('SMTP Logout');
  39.        if not smtp.Logout() then raise ESMTP.Create('SMTP ERROR: Logout:' + smtp.EnhCodeString);
  40.       AddToLog('OK!');
  41.   finally
  42.     smtp.Free;
  43.     msg_lines.Free;
  44.   end;
  45. end;

I have no other information regarding SendTo, except that simply it fails.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Synapse SMTPSend Subject Field
« Reply #8 on: February 02, 2022, 07:51:57 am »
I have no other information regarding SendTo, except that simply it fails.

I'm guessing that despite saying "my mail server" you do not have access to the mail server log?

If this is the case, PM me and I'll give you an email address to one of the mail servers I control.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1311
    • Lebeau Software
Re: Synapse SMTPSend Subject Field
« Reply #9 on: February 02, 2022, 06:34:26 pm »
But I've tested following codes, which displays up to "OK"

That means no errors are being reported by the SMTP server you are connected to.  Which means the server has accepted the email, and it is server's responsibility to deliver it.  It is out of your hands.

but still there are no mails received.

Not all SMTP failures can be reported in real-time while you are still connected to the SMTP server.  Sometimes failures are only detected after you have finished sending commands and the SMTP server tries to relay the email.  Did you check your email inbox to see if any error messages were sent back to you?

I have no other information regarding SendTo, except that simply it fails.

Then there is no way to diagnose your problem.  Can you please capture the raw SMTP commands and responses?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

egsuh

  • Hero Member
  • *****
  • Posts: 1266
Re: Synapse SMTPSend Subject Field
« Reply #10 on: February 05, 2022, 12:05:59 pm »
Quote
I'm guessing that despite saying "my mail server" you do not have access to the mail server log?

I'm using SMTP services of commercial Email service providers. They are not under my control, exactly. I'll mail to you if necessary.

Quote
Then there is no way to diagnose your problem.  Can you please capture the raw SMTP commands and responses?

I do not know how to capture raw SMTP commands and responses.

Currently I can send emails using SendToEx anyway. Bigger problem is character encodings are not maintained when mails sent via my application are not maintained when called from MS Outlook (IPhone mail is fine).

Currently I'm tied up with other issues, so cannot go deeper into this issue. When available, I'll post my new understandings here. And write on the character encoding issue as separate thread.

Thank you for your helps.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1311
    • Lebeau Software
Re: Synapse SMTPSend Subject Field
« Reply #11 on: February 07, 2022, 12:16:41 am »
I do not know how to capture raw SMTP commands and responses.

Use a packet sniffer, like Wireshark.  Unless the connection is encrypted with SSL/TLS, then that won't work.  I would be very surprised if Synapse doesn't provide a way to log the raw unencrypted data it is transmitting and receiving.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018