Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get email from outlook with delphi

I was wondering if anyone knows how to get emails from outlook for example with the help of Delphi code. What I'd like to get is every part of the email like, subject, sender, attachments etc.

Best Regards!

like image 352
Joe Avatar asked May 08 '11 21:05

Joe


2 Answers

This example shows you how to use the TOutlookApplication component provided with Delphi to send emails with Outlook. It should get you an idea on what information is available for mail items.

Detecting mailboxes in outlook will tell you where you can find the emails that are currently in any Outlook mail boxes.

function Send: boolean;
var
  Outlook: TOutlookApplication;
  olNameSpace: NameSpace;
  MailIt: TMailItem;
  AttachedFile: OleVariant;
  i: integer;
  emailaddress: string;
begin
  Result := true;
  Outlook := TOutlookApplication.Create( nil );
  try
    Outlook.ConnectKind := ckNewInstance;
    try
      Outlook.Connect;
      try
        olNameSpace := Outlook.GetNamespace('MAPI');
        olNameSpace.Logon('', '', False, False);
        try

          for i := 0 to FNewUsers.Count - 1 do begin
            MailIt := TMailItem.Create( nil );
            MailIt.ConnectTo( Outlook.CreateItem( olMailItem ) as MailItem );
            try
              emailaddress := TStapper( FNewUsers.Items[i] ).Email;
              if emailaddress = '' then begin
                emailaddress := C_MailUnknownAddress;
              end;
              MailIt.Recipients.Add( emailaddress );
              MailIt.Subject := C_MailSubject;
              MailIt.Body := Format( C_MailBody,
                  [TStapper( FNewUsers.Items[i] ).UserId,
                  TStapper( FNewUsers.Items[i] ).Password] );
              MailIt.Save;
            finally
              MailIt.Free;
            end;
          end;

        finally
          olNameSpace.Logoff;
        end;
      finally
        Outlook.Disconnect;
      end;
    finally
      Outlook.free;
    end;
  except
    on E: Exception do begin
      Result := false;
    end;
  end;
end;
like image 93
Marjan Venema Avatar answered Nov 16 '22 19:11

Marjan Venema


You can use standard OLE automation to access Outlook:

var 
  Outlook: OLEVariant;
begin
  try
   Outlook:=GetActiveOleObject('Outlook.Application') ;
  except
   Outlook:=CreateOleObject('Outlook.Application') ;
  end;
  //...
end;

You may also have a look at TurboPower OfficePartner which is an easy way to integrate with Office. I haven't looked at this project for ages, so it might be outdateed, but on the front page the latest activity was only a few months ago...

like image 36
Jørn E. Angeltveit Avatar answered Nov 16 '22 20:11

Jørn E. Angeltveit