如何在IMAP邮箱中搜索特定的电子邮件? [英] How to search for a specific e-mail message in IMAP mailbox?

查看:182
本文介绍了如何在IMAP邮箱中搜索特定的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据邮件中包含的特定文本检索特定的电子邮件?例如,Gmail搜索的工作方式。如果您搜索电子邮件中的特定文本,则Gmail将检索与该文本关联的消息。最好没有任何循环。

How can I retrieve a specific e-mail based on a certain text contained in the message ? For example how Gmail search works. If you search for a specific text, which is in the e-mail, then the Gmail will retrieve the message that is associated with the text. Preferably without any looping.

推荐答案

您正在寻找 SearchMailBox 方法。这是一个简单的示例,期望您具有IMAP客户端(在这种情况下,IMAPClient 变量/html/TIdIMAP4.html rel = nofollow noreferrer> TIdIMAP4 类型)已连接到Gmail服务器。对于那些寻求这样做的人,请查看 此帖子 放在 IMAPClient.Connect try..finally 块中c>和 IMAPClient.Disconnect

You're looking for the SearchMailBox method. Here's a simple example expecting that you have the IMAP client (in this case, the IMAPClient variable of the TIdIMAP4 type) already connected to the Gmail server. For those looking for how to do so, take a look for instance at this post and put this code inside the try..finally block near IMAPClient.Connect and IMAPClient.Disconnect.

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;

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

Here's the quick implementation of the IMAP search for UTF-8 charset. It uses interposed class due to protected ParseSearchResult method:

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;

这篇关于如何在IMAP邮箱中搜索特定的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆