Easiest way to compose Outlook 2010 mail from Delphi?

后端 未结 5 756
别跟我提以往
别跟我提以往 2021-01-01 06:48

Some of our applications which work fine with different ways of email integration, using mailto:, simulated \"Send To...\", and SMTP in Windows 2000 and 2003 en

5条回答
  •  醉梦人生
    2021-01-01 07:15

    I dug up this example. It's from 2002, so uses an older outlook version, but the idea should still be the same. It was used in an application to send newly registered users their userid and password (don't start me on that, it was just a way to restrict access to a site, nothing sensitive there). The messages are saved in the Outbox, not opened, but if you dig through the OleServer and Outlook8 (number will be higher by now), you should find ways to open the mail for user attention instead of saving it straight to the outbox.

    The entire Outlook object model can be found on msdn: http://msdn.microsoft.com/en-us/library/aa221870%28v=office.11%29.aspx

    Another good source for information about Office Automation are Deborah Pate's pages: http://www.djpate.freeserve.co.uk/Automation.htm

    function TStapWerkt_data.GenerateMailsOutlook(SendDBF: boolean): 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
    
              if SendDBF then begin
                MailIt := TMailItem.Create( nil );
                MailIt.ConnectTo( Outlook.CreateItem( olMailItem ) as MailItem );
                try
                  MailIt.Recipients.Add( 'info@bjmsoftware.com' );
                  MailIt.Subject := 'StapWerk.dbf';
                  MailIt.Body := 'Stap'#13#10;
                  AttachedFile := IncludeTrailingBackslash( ExtractFilePath(
                      Stap_db.Stap_Database.DatabaseName ) ) + 'stapwerk.dbf';
                  MailIt.Attachments.Add( AttachedFile, EmptyParam, EmptyParam, EmptyParam );
                  MailIt.Save;
                finally
                  MailIt.Free;
                end;
              end;
    
              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;
            // We kunnen ondanks ckNewInstance quit niet gebruiken
            // Outlook lijkt de connectkind te negeren en hoe dan ook te koppelen
            // naar een bestaande instantie. En die gooi je dan dus ook dicht met quit.
    //        Outlook.quit;
          end;
        finally
          Outlook.free;
        end;
      except
        on E: Exception do begin
          Result := false;
        end;
      end;
    end;
    

提交回复
热议问题