* * *

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

UglyBloke

  • New member
  • *
  • Posts: 6
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

  • Sr. Member
  • ****
  • Posts: 333
    • 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) open source project - Admin, Developer

UglyBloke

  • New member
  • *
  • Posts: 6
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.

UglyBloke

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

Remy Lebeau

  • Sr. Member
  • ****
  • Posts: 333
    • 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) open source project - Admin, Developer

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus