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?).
You obviously did not read the
TIdMessage.Recipients
Identifies the recipients of a message.
property Recipients: TIdEmailAddressList;
Description
Recipients is aTIdEMailAddressList
property used to storeTIdEmailAddressItem
values that identify the recipients of the message. UseCCList
for recipients to receive a Carbon Copy of the message. UseBCCList
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.