Lazarus/Free Pascal Example Using SSL to Access POP Server (Windows)

This covers Lazarus/POP/SSL in Windows. Please go here if you are using a Raspberry Pi.

I need to write a command utility that scans a POP mailbox looking for a particular message so it can act. It will be primarily used to extract email from gmail which requires a SSL encrypted connection.

There is a library for Free Pascal, Synapse, that will handle this. It took a while to find an example of how to do this, but there is one on the Synapse website.

The example is pretty straight forward and does work, but what is not clear is that you need some underlying libraries to make the example work.

You will need the following DLL’s. You can place them in the same directory as the executable or in the system32 directory.

  • libeay32.dll
  • ssleay32.dll
  • msvcr71.dll

MSVCR71.DLL is a microsoft library. There is a good chance it will be in your system32 directory already. If not, you will almost certainly find it somewhere on your PC. I had many instances of it.

If you look on the download page of the Synapse website, down toward the bottom is mention of ‘Crypting libraries for SSL/TLS/SSH in Synapse‘. There you will find openssl-0.9.8d-win32.zip. From message threads I’ve read, it is my understanding you must use THAT version of openssl with the current version of Synapse. Download the zip file and you will find the two missing DLL’s.

Here is my sample code. It is very close to Synapse’s code except I look at the port being used before opening the mailbox. If the port is 110, I will not use SSL; otherwise, I do. This code has worked successfully on gmail, yahoo, and a private POP server.

program popExample;

{$mode objfpc}{$H+}

uses
    {$IFDEF UNIX}{$IFDEF UseCThreads}
    cthreads,
    {$ENDIF}{$ENDIF}
    Classes,
    mimemess,
    pop3Send,
    ssl_openssl;

{$R *.res} 

var

    k                           : integer;
    msg                         : TMimeMess;
    popMbx                      : TPOP3Send;
    tsl                         : TStringList;

begin

popMbx := TPOP3Send.create;
tsl    := TStringList.create;
msg    := TMimeMess.create;

// setup for connect and login
popMbx.TargetHost           := 'pop.gmail.com';
popMbx.Username             := 'user@gmail.com';
popMbx.Password             := 'password';
popMbx.TargetPort           := '995'; {or '110'}
popMbx.AuthType             := POP3AuthAll;
if (popMbx.TargetPort <> '110') then begin                  // set options for SSL access
    popMbx.AutoTLS          := false;
    popMbx.FullSSL          := true;
    popMbx.Sock.SSLDoConnect();
    end
else begin                                                  // set options for non-SSL
    popMbx.AutoTLS          := true;
    end;

if (not popMbx.login) then begin                            // login to mbx
    writeln('POP login failed.');
    halt(1);
    end;

writeln('check capa');                                      // test capa command
popMbx.Capability;
for k := 0 to popMbx.fullresult.Count-1 do begin
    writeln(popMbx.fullresult.Strings[k]);
    end;

writeln('output from list');                                // test list command
popMbx.list(0);
for k := 0 to popMbx.fullresult.Count-1 do begin
    writeln(popMbx.fullresult.Strings[k]);
    tsl.Add(popMbx.fullresult.Strings[k]);
    end;

writeln('uidl 0');                                          // test uidl command
popMbx.Uidl(0);
for k := 0 to popMbx.fullresult.Count-1 do begin
    writeln(popMbx.fullresult.Strings[k]);
    end;

writeln('uidl 1');
popMbx.Uidl(1);
for k := 0 to popMbx.fullresult.Count-1 do begin
    writeln(popMbx.fullresult.Strings[k]);
    end;

popMbx.Stat;                                                // print from/subject of everything in mbx
writeln('Message Count: ', popMbx.StatCount);
for k := 1 to popMbx.statcount do begin
    writeln('Processing msg ', k);
    popMbx.Retr(k);
    msg.Clear;
    msg.Lines.Assign(popMbx.FullResult);                    // copy msg from popmbx to msg
    msg.DecodeMessage;
    writeln('  from:       ', msg.Header.From);
    writeln('  subject:    ', msg.Header.Subject);
    end;

popMbx.logout;
popMbx.free;

end.

 

This entry was posted in c-lazarus, c-pcos and tagged . Bookmark the permalink.

One Response to Lazarus/Free Pascal Example Using SSL to Access POP Server (Windows)

  1. Pingback: Lazarus/Free Pascal Example Using SSL to Access POP Server (Linux/Raspberry Pi) | Big Dan the Blogging Man

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s