Lazarus/Free Pascal Example Using SSL to Access POP Server (Linux/Raspberry Pi)

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

As already mentioned: 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.

For the linux implementation of Synapse, you will need the external linux libraries libssl.so and libcrypto.so. They are in the package libssl-dev. To install this package:

sudo apt-get install libssl-dev

With those libraries installed, the sample code should run fine.

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.

 

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

One Response to Lazarus/Free Pascal Example Using SSL to Access POP Server (Linux/Raspberry Pi)

  1. Pingback: Lazarus/Free Pascal Example Using SSL to Access POP Server (Windows) | 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