[Lazarus] Email sending program using Indy stopped working - seems to be an SSL problem

Michael Van Canneyt michael at freepascal.org
Sun Jan 14 12:54:08 CET 2024



On Sun, 14 Jan 2024, Bo Berglund via lazarus wrote:

> On Sat, 13 Jan 2024 17:03:55 +0100 (CET), Michael Van Canneyt via lazarus
> <lazarus at lists.lazarus-ide.org> wrote:
>
>> You could also use Synapse. I always prefer synapse over Indy.
>>
>
> So I have now verified that I cannot use Indy10 for email sending anymore :( ...
>
> I have used Indy since a very long time like 20 years or so when dealing with
> Internet accesses. So I am not used to other ways.
>
> Now I have looked around for valid examples which will work like my send
> function but using Synapse but the result is confusing.
>
> I have found your pdf document "Sending mails using Lazarus.pdf".
>
> But I am not clear as to how to map the Indy properties to Synapse...
> And the example's uses clause is not shown so I don't know what to put there...

It says so in the text: 'the smtpsend unit'.

>
> In fact I have looked for Synapse and in OnLine Package Manager I find
> Synapse_40.1, which I have installed via OLPM. But when looking through the
> packages in Lazarus I cannot find any match to synapse at all.
> What have I done wrong?

Nothing.

The synapse package is not installed in the IDE, it does not
install any components on the component palette.

The Synapse package just offers classes which you must create in code.

But once you compiled the package, you can specify it as a dependency.

This is explained in my article, in the first paragraphs.

>
> Inside the pdf I found a link to http://synapse.ararat.cz/ but there seems to be
> only files from more than 10 years ago, how could they solve the recently
> encountered ssl problem?

The latest code is on sourceforge:

https://sourceforge.net/p/synalist/code/HEAD/tree/trunk/

You can see it referenced on:

http://synapse.ararat.cz/doku.php/download

The maintainer is considering switching to git(lab|hub), but this will take
some time.

However, the package in lazarus should be up-to-date for your needs, I think.

> Could you (or someone else reading this) suggest a new SendSvnMessage function
> that will replace the following (snippets) but using Synapse (and say from where
> I can get synapse):

The following program compiles:

With the aid of my article, you should be able to extend the code to work with attachments etc.

----
program sm;

{$mode objfpc}
{$h+}

uses
   ssl_openssl, smtpSend, mimepart, mimemess, sysutils, classes;

type
   { TSvnUser } //Data from SVN user config is put here
   TSvnUser = class
   public
     LoginName,
     FullName,
     Email: string;
   end;
   TSvnUsers = Array of TSVNUser;


   { TSvnMessage }
   TSvnMessage = class
   private
     FSMTP: TSMTPSend;
     FMailMessage: TMimeMess;
     FSender : String;
     FMailServer: string;
     FMailLogin: string;
     FMailPwd: string;
     FMailPort: word;
     FMailUseSSL: boolean;
     FMailTimeout: integer;
     FSvnUsers: TSvnUsers; //A list of users (TSvnUser) and data
     function SendSvnMessage: boolean;
     procedure preparemessage;
   end;

   Procedure Log(Msg : string);


   begin
     writeln(msg)
   end;

   Procedure MailError(Msg : String);

   begin
     Raise Exception.Create(Msg);
   end;

   procedure LogException(Msg : string);

   begin
     Writeln('Exception occurred : ',Msg);
   end;

function TSvnMessage.SendSvnMessage: boolean;


var
   sSubject: string;
   i: integer;
begin
   Result := false;
   try
     PrepareMessage;
     //Set up the SMTP transfer properties
     FSMTP.TargetPort := IntToStr(FMailPort);
     FSMTP.TargetHost := FMailServer;
     FSMTP.AutoTls:=true;
     FSMTP.Username := FMailLogin;
     FSMTP.Password := FMailPwd;
//    FSMTP.MailAgent := 'SVNMailer';
     FSMTP.Timeout := FMailTimeout;
     // compose the message
     FMailMessage.EncodeMessage;

     //Now send message
     Log('Connecting to mailserver');
     if FSMTP.Login then
     begin
       Log('Sending message');
       if not FSMTP.MailFrom(FSender,0) then
         MailError('Setting mail from')
       else
         begin
         if not FSMTP.MailTo('user at example.com') then
           MailError('Setting mail to')
         else
           FSMTP.MailData(FMailMessage.Lines);
         end;
       Result := true;
     end;
   except
     on E: Exception do
     begin
       LogException('In SendSvnMessage = ' + E.Message);
     end;
   end;
end;

procedure TSvnMessage.PrepareMessage;
var
 	i: integer;
   Usr,
   Msg: string;
   SU: TSvnUser;

begin
   (*if Pos('<html>', FMailMessage.Body.Text) > 0 then
     FMailMessage.ContentType := 'text/html; charset=utf-8'
   else
     FMailMessage.ContentType := 'text/plain; charset=utf-8';*)
   Msg := 'Recipients:';
//  for i := 0 to FSubscription.Count - 1 do
   begin
//    Usr := Lowercase(FSubscription[i]);
//    if Pos('#', Usr) > 0 then Continue;
//    SU := FSvnUsers.UserByLogin[Usr];
     if SU <> NIL then
     begin
       // Add to TO: list. There is also CClist

       FMailMessage.Header.ToList.Add(Format('%s <%s>',[SU.FullName,SU.Email]));
       Msg := Msg + ' ' + Usr;
     end;
   end;
//  Log(Msg);
end;

begin
end.


More information about the lazarus mailing list