2012-11-28 30 views
5

संदेश में निहित किसी निश्चित पाठ के आधार पर मैं एक विशिष्ट ई-मेल कैसे प्राप्त कर सकता हूं? उदाहरण के लिए जीमेल खोज कैसे काम करती है। यदि आप एक विशिष्ट पाठ की खोज करते हैं, जो कि ई-मेल में है, तो जीमेल पाठ से जुड़े संदेश को पुनः प्राप्त करेगा। किसी भी लूपिंग के बिना अधिमानतः।आईएमएपी मेलबॉक्स में एक विशिष्ट ई-मेल संदेश कैसे खोजें?

उत्तर

4

आप SearchMailBox विधि की तलाश में हैं। यहां एक साधारण उदाहरण दिया गया है कि आपके पास IMAP क्लाइंट है (इस मामले में, TIdIMAP4 प्रकार का चर) पहले से ही जीमेल सर्वर से जुड़ा हुआ है। उन लोगों के लिए जो ऐसा करने की तलाश में हैं, उदाहरण के लिए this post और परtry..finally ब्लॉक IMAPClient.Connect और 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; 

यहां यूटीएफ -8 वर्णसेट के लिए आईएमएपी खोज का त्वरित कार्यान्वयन है।

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; 
+0

इस वापसी HTML या शुद्ध पाठ करता है यह संरक्षित ParseSearchResult विधि की वजह से interposed वर्ग का उपयोग करता है? क्योंकि यदि आप ईमेल में कुछ खोजते हैं जो शुद्ध पाठ हैं तो यह ठीक काम करता है। अगर वे HTML खोज हैं तो काम नहीं करेंगे। –

+0

यह ['SEARCH'] (http://tools.ietf.org/html/rfc1730#section-6.4.4) कमांड के लक्षित IMAP सर्वर कार्यान्वयन पर निर्भर करता है, लेकिन इस मामले में यह टेक्स्ट संदेश में नहीं खोजता है दुर्भाग्य से भागों। उल्लेख करने के लिए अच्छा बिंदु। – TLama

+0

मुझे लगता है कि जीमेल सेटिंग्स में शुद्ध एचटीएमएल या पाठ वापस करने के लिए सेट करने का एक तरीका हो सकता है। –