加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何在IMAP邮箱中搜索特定的电子邮件?

发布时间:2020-12-15 04:13:31 所属栏目:大数据 来源:网络整理
导读:如何根据邮件中包含的特定文本检索特定电子邮件?例如Gmail搜索的工作原理.如果您搜索电子邮件中的特定文本,则Gmail将检索与该文本关联的邮件.优选地,没有任何循环. 解决方法 您正在寻找 SearchMailBox 方法.这是一个简单的示例,期望您已经连接到Gmail服务器
如何根据邮件中包含的特定文本检索特定电子邮件?例如Gmail搜索的工作原理.如果您搜索电子邮件中的特定文本,则Gmail将检索与该文本关联的邮件.优选地,没有任何循环.

解决方法

您正在寻找 SearchMailBox方法.这是一个简单的示例,期望您已经连接到Gmail服务器的IMAP客户端(在本例中为 TIdIMAP4类型的IMAPClient变量).对于那些寻找如何操作的人,请查看例如 this post并将此代码放在IMAPClient.Connect和IMAPClient.Disconnect附近的try..finally块中.
var
  // in this example is not shown how to connect to Gmail IMAP server but
  // it's expected that the IMAPClient object is already connected there
  IMAPClient: TIdIMAP4;

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  MsgObject: TIdMessage;
  SearchInfo: array of TIdIMAP4SearchRec;
begin
  // if the mailbox selection succeed,then...
  if IMAPClient.SelectMailBox('INBOX') then
  begin
    // set length of the search criteria to 1
    SetLength(SearchInfo,1);
    // the SearchKey set to skBody means to search only in message body texts
    // for more options and explanation,see comments at the TIdIMAP4SearchKey
    // enumeration in the IdIMAP4.pas unit
    SearchInfo[0].SearchKey := skBody;
    // term you want to search
    SearchInfo[0].Text := 'Search term';

    // if the search in the selected mailbox succeed,then...
    if IMAPClient.SearchMailBox(SearchInfo) then
    begin
      // iterate the search results
      for I := 0 to High(IMAPClient.MailBox.SearchResult) do
      begin
        // make an instance of the message object
        MsgObject := TIdMessage.Create(nil);
        try
          // try to retrieve currently iterated message from search results
          // and if this succeed you can work with the MsgObject
          if IMAPClient.Retrieve(IMAPClient.MailBox.SearchResult[I],MsgObject) then
          begin
            // here you have retrieved message in the MsgObject variable,so
            // let's do what what you need with the >> MsgObject <<
          end;
        finally
          MsgObject.Free;
        end;
      end;
    end;
  end;
end;

这是IMF搜索UTF-8字符集的快速实现.由于受保护的ParseSearchResult方法,它使用插入的类:

type
  TBasicSearchKey = (bskBcc,bskBody,bskCc,bskFrom,bskHeader,bskKeyword,bskSubject,bskText,bskTo);
const
  IMAPSearchKeys: array [TBasicSearchKey] of string = ('BCC','BODY','CC','FROM','HEADER','KEYWORD','SUBJECT','TEXT','TO');
type
  TIdIMAP4 = class(IdIMAP4.TIdIMAP4)
  public
    function SearchMailBoxUTF8(const ASearchText: string;
      ASearchKey: TBasicSearchKey): Boolean;
  end;

implementation

{ TIdIMAP4 }

function TIdIMAP4.SearchMailBoxUTF8(const ASearchText: string;
  ASearchKey: TBasicSearchKey): Boolean;
var
  SearchText: RawByteString;
begin
  Result := False;
  CheckConnectionState(csSelected);

  SearchText := UTF8Encode(ASearchText);
  SendCmd(Format('SEARCH CHARSET UTF-8 %s {%d}',[IMAPSearchKeys[ASearchKey],Length(SearchText)]),['SEARCH']);
  if LastCmdResult.Code = IMAP_CONT then
    IOHandler.WriteLn(SearchText,TEncoding.UTF8);

  if GetInternalResponse(LastCmdCounter,['SEARCH'],False) = IMAP_OK then
  begin
    ParseSearchResult(FMailBox,LastCmdResult.Text);
    Result := True;
  end;
end;

用法:

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  MsgObject: TIdMessage;
begin
  if IMAPClient.SelectMailBox('INBOX') and
    IMAPClient.SearchMailBoxUTF8('Search term',bskText) then
  begin
    for I := 0 to High(IMAPClient.MailBox.SearchResult) do
    begin
      MsgObject := TIdMessage.Create(nil);
      try
        if IMAPClient.Retrieve(IMAPClient.MailBox.SearchResult[I],MsgObject) then
        begin
          // here you have retrieved message in the MsgObject variable,so
          // let's do what what you need with the >> MsgObject <<
        end;
      finally
        MsgObject.Free;
      end;
    end;
  end;
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读