Receive emails from Outlook with delphi

I was wondering if anyone knows how to receive emails from Outlook, for example using Delphi code. What I would like to receive is each piece of email, for example, subject, sender, attachments, etc.

Regards!

+5
source share
2 answers

This example shows how to use the TOutlookApplication component that ships with Delphi to send email with Outlook. This should give you an idea of ​​what information is available for mail.

Outlook , , Outlook.

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;
+4

OLE Outlook:

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

TurboPower OfficePartner, Office. , , ...

+2

All Articles