Recent

Author Topic: Imap example  (Read 11416 times)

Roosters

  • New Member
  • *
  • Posts: 16
Imap example
« on: March 16, 2012, 03:43:59 pm »
I've spent hours searching for an answer to this question, but have not come close to a solution, i hope someone can help.

I need to poll a mailserver for new emails every few seconds (or use imap idle/push), if the subject line matches a certain string, send the body text to a receipt printer, then move the email to another folder.  It needs no more functionality than that.

I would prefer to use synapse, but Indy or Lnet would suffice.

Every example I've come across (and there are very very few of those) doesn't show how to check for new messages, (that I think I can work around), but the one big sticking point is decoding the mime content and printing the text/body of the email.

Can anyone help with some simple example code ?




ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Imap example
« Reply #1 on: March 17, 2012, 03:01:12 pm »
A simple app that prints all new messages in a memo. It shows how to access the individual decoded elements.

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  imapsend, ssl_openssl,mimemess,mimepart;

type

  { TForm1 }

  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
  imap:TIMAPSend;
  i,j,cnt,spcnt:integer;
  FolderList: TStringList;
  MimeMess:TMimeMess;
  MimePart:TMimePart;
  s:string;
begin
  imap:=TIMAPSend.create;
  FolderList:=TStringList.Create;
  MimeMess:=TMimeMess.Create;
  try
    imap.TargetHost:='192.168.2.99';
    imap.UserName:='user';
    imap.Password:='password';
    imap.AutoTLS:=true;
    if imap.Login then
      begin
      //get all folders
      imap.List('',FolderList);
      for i:=0 to FolderList.Count-1 do
        begin
        // are there any unread messages?
        cnt:=imap.StatusFolder(FolderList[i],'UNSEEN');
        if cnt>0 then
          begin
          imap.SelectROFolder(FolderList[i]);
          // get last SelectedRecent messages
          for j:=imap.SelectedCount-imap.SelectedRecent to imap.SelectedCount do
            begin
            imap.FetchMess(j,MimeMess.Lines);
            // deocde header and body
            MimeMess.DecodeMessage;
            Memo1.Lines.Add('--------------------------------------------------');
            Memo1.Lines.Add(MimeMess.Header.From);
            Memo1.Lines.Add(MimeMess.Header.Subject);
            // is this multipart
            spcnt:=MimeMess.MessagePart.GetSubPartCount();
            if spcnt>0 then
              //get all parts
              for cnt:=0 to spcnt-1 do
                begin
                MimePart:=MimeMess.MessagePart.GetSubPart(cnt);
                MimePart.DecodePart;
                Memo1.Lines.Add('-----------------------');
                Memo1.Lines.Append(MimePart.Primary+' ; '+MimePart.Secondary);
                Memo1.Lines.Add('-----------------------');
                setlength(s,MimePart.DecodedLines.Size);
                MimePart.DecodedLines.Read(s[1],length(s));
                Memo1.Lines.Add(s);
                end
            else
              //print body
              Memo1.Lines.AddStrings(MimeMess.MessagePart.Lines);
            end;
          end;
        end;
      end;
  finally
    MimeMess.Free;
    FolderList.Free;
    imap.free;
  end;
end;

end.

The code could use better handling ;)


Roosters

  • New Member
  • *
  • Posts: 16
Re: Imap example
« Reply #2 on: March 18, 2012, 07:15:55 am »
Thank you very much, really appreciated, helps me enormously.


Do you know if it's possible for synapse to use imap idle ?.


Thanks again.




 

TinyPortal © 2005-2018