简体   繁体   中英

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?).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM