Search code examples
delphiindy10

How to extract all email address from an Indy TIdMessage object?


Using Indy, how can I extract all email addresses that are present in the To, Cc and Bcc fields of a TIdMessage? As these fields can contain more than one address, I must parse them, but I didn't find any ready-made function for that (maybe I've missed it?).


Solution

  • You obviously did not read the

    TIdMessage documentation:

    TIdMessage.BccList

    TIdMessage.CCList

    TIdMessage.Recipients
    Identifies the recipients of a message.

    property Recipients: TIdEmailAddressList;

    Description
    Recipients is a TIdEMailAddressList property used to store TIdEmailAddressItem values that identify the recipients of the message. Use CCList for recipients to receive a Carbon Copy of the message. Use BCCList for recipients to receive a Blind Carbon Copy of the message.

    All of these properties give you a TIdEmailAddressList that you can harvest for addresses.

    This is the second item in a google search for Indy TIdMessage.

    For example:

    function GetEmailAddresses(const Email: TIdMessage): TStringList;
    var
      Item: TIdEmailAddressItem;
    begin
      Result := TStringList.Create;
      for Item in Email.Recipients do Result.Add(Item.Address);
      for Item in Email.CcList do Result.Add(Item.Address);
      for Item in Email.BccList do Result.Add(Item.Address);
    end;
    

    Note that the Indy documentation uses the with keyword a lot.
    Although convenient, using with is a very bad idea and I recommend you avoid it at all costs.