Synapse SMTPSend Subject Field lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Jacob21 · 2017-04-26 19:09 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 · 2017-04-26 20:50 Quote from: UglyBloke on April 26, 2017, 09:09:47 pmCould 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" ');     Email.Add('To: "You" ');     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" ';     Email.Header.ToList.Add('"You" ');     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 ---------------------------------------------------------------------- Jacob21 · 2017-04-26 23:36 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 · 2017-04-28 11:01 Deleted Synapse, Loaded Indy10 Now working 100% ---------------------------------------------------------------------- Remy Lebeau · 2017-04-28 18:45 Quote from: UglyBloke on April 27, 2017, 01:36:15 amMy 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" ');     Email.Add('To: "You" ');     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" ';     Email.Header.ToList.Add('"You" ');     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" ');     Email.Add('To: "You" ');     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" ';     Email.Header.ToList.Add('"You" ');     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; ---------------------------------------------------------------------- egsuh · 2022-02-01 08:56 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 · 2022-02-01 21:19 Quote from: egsuh on February 01, 2022, 09:56:10 amsmtpSend.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? Quote from: egsuh on February 01, 2022, 09:56:10 amsmtpSend.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? ---------------------------------------------------------------------- egsuh · 2022-02-02 05:14 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure MailSend(const sSmtpHost, sSmtpPort, sSmtpUser, sSmtpPasswd,                     sFrom, sTo, Content: String);var  smtp: TSMTPSend;  msg_lines: TStringList;   procedure AddtoLog(s:string);  begin     if Assigned(OutStrings) then OutStrings.Append(s);  end; begin  msg_lines := TStringList.Create;  smtp := TSMTPSend.Create;  try      msg_lines.Text := Content;       smtp.UserName := sSmtpUser;      smtp.Password := sSmtpPasswd;       smtp.TargetHost := sSmtpHost;    smtp.TargetPort := sSmtpPort;       SMTP.AutoTLS:= false; // true;    SMTP.FullSSL:=Trim(sSMTPPort)<>'25';  // if sending to port 25, don't use encryption       AddToLog('SMTP Login');      if not smtp.Login() then  raise ESMTP.Create('SMTP ERROR: Login:' + smtp.EnhCodeString);    {      AddToLog('SMTP StartTLS');      if not smtp.StartTLS() then raise ESMTP.Create('SMTP ERROR: StartTLS:' + smtp.EnhCodeString); }     AddToLog('SMTP Mail');      if not smtp.MailFrom(sFrom, Length(sFrom)) then  raise ESMTP.Create('SMTP ERROR: MailFrom:' + smtp.EnhCodeString);      if not smtp.MailTo(sTo) then  raise ESMTP.Create('SMTP ERROR: MailTo:' + smtp.EnhCodeString);      if not smtp.MailData(msg_lines) then  raise ESMTP.Create('SMTP ERROR: MailData:' + smtp.EnhCodeString);        AddToLog('SMTP Logout');     if not smtp.Logout() then raise ESMTP.Create('SMTP ERROR: Logout:' + smtp.EnhCodeString);      AddToLog('OK!');  finally    smtp.Free;    msg_lines.Free;  end;end; I have no other information regarding SendTo, except that simply it fails. ---------------------------------------------------------------------- trev · 2022-02-02 06:51 Quote from: egsuh on February 02, 2022, 06:14:52 amI 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 · 2022-02-02 17:34 Quote from: egsuh on February 02, 2022, 06:14:52 amBut 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. Quote from: egsuh on February 02, 2022, 06:14:52 ambut 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? Quote from: egsuh on February 02, 2022, 06:14:52 amI 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? ---------------------------------------------------------------------- egsuh · 2022-02-05 11:05 QuoteI'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. QuoteThen 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 · 2022-02-06 23:16 Quote from: egsuh on February 05, 2022, 12:05:59 pmI 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.