5
メッセージに含まれる特定のテキストに基づいて特定の電子メールを取得するにはどうすればよいですか?たとえば、Gmail検索の仕組み電子メールにある特定のテキストを検索すると、Gmailはそのテキストに関連付けられたメッセージを取得します。好ましくはループなし。IMAPメールボックスで特定の電子メールメッセージを検索するにはどうすればよいですか?
メッセージに含まれる特定のテキストに基づいて特定の電子メールを取得するにはどうすればよいですか?たとえば、Gmail検索の仕組み電子メールにある特定のテキストを検索すると、Gmailはそのテキストに関連付けられたメッセージを取得します。好ましくはループなし。IMAPメールボックスで特定の電子メールメッセージを検索するにはどうすればよいですか?
あなたはSearchMailBox
メソッドを探しています。 IMAPクライアント(この場合は、TIdIMAP4
タイプのIMAPClient
変数)が既にGmailサーバーに接続されていることを想定して、簡単な例を示します。その方法をお探しの方は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;
ここでは、UTF-8文字セットのIMAP検索を簡単に実装しています。これは、保護された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;
は、この戻りHTMLや純粋なテキストをしていますか?なぜなら純粋なテキストである電子メールの中の何かを検索すればOKです。それらがHTML検索であれば動作しません。 –
[[SEARCH]](http://tools.ietf.org/html/rfc1730#section-6.4.4)コマンドのターゲットIMAPサーバーの実装に依存しますが、この場合はテキストメッセージで検索しません残念ながら部品。言及に良いポイント。 – TLama
Gmailの設定で純粋なhtmlやテキストを返すように設定する方法があると思います。 –