Get email headers from GMail's “Sent items” folder

后端 未结 1 641
逝去的感伤
逝去的感伤 2021-01-23 21:25

My program sends emails to contacts via GMail. Normally this works very well but we have noticed that sometimes an email which my program \"thinks\" it has sent doesn\'t actuall

1条回答
  •  情书的邮戳
    2021-01-23 22:11

    To get the info related to the Sent Items you can use the Gmail imap_extensions and the TIdIMAP4 component.

    Try this sample

    {$APPTYPE CONSOLE}
    
    
    uses
      Classes,
      SysUtils,
      IdIMAP4,
      IdSSLOpenSSL,
      IdMessageCollection,
      IdExplicitTLSClientServerBase;
    
    procedure GetSentItems;
    var
      LIdIMAP4: TIdIMAP4;
      LIdSSLIOHandlerSocketOpenSSL : TIdSSLIOHandlerSocketOpenSSL;
      AMailBoxList: TStrings;
      AMsgList: TIdMessageCollection;
      i: integer;
    begin
      LIdIMAP4 := TIdIMAP4.Create(nil);
      try
        LIdSSLIOHandlerSocketOpenSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
        try
          LIdSSLIOHandlerSocketOpenSSL.SSLOptions.Method := sslvSSLv3;
          LIdIMAP4.IOHandler := LIdSSLIOHandlerSocketOpenSSL;
          LIdIMAP4.Host := 'imap.gmail.com';
          LIdIMAP4.Port := 993;
          LIdIMAP4.UseTLS := utUseImplicitTLS;
          LIdIMAP4.Username := 'user';
          LIdIMAP4.Password := 'password';
          LIdIMAP4.Connect;
          try
            //list the mail boxes 
            AMailBoxList:=TStringList.Create;
            try
            if LIdIMAP4.ListSubscribedMailBoxes(AMailBoxList) then
             Writeln(AMailBoxList.Text);
            finally
              AMailBoxList.Free;
            end;
    
            AMsgList:=TIdMessageCollection.Create(TIdMessageItem);
            try
            if LIdIMAP4.SelectMailBox('[Gmail]/Enviados') then //This folder name is localizated in english use [Gmail]/Sent Mail
              if LIdIMAP4.MailBox.TotalMsgs>0 then
                if LIdIMAP4.UIDRetrieveAllEnvelopes(AMsgList) then
                 for i := 0 to AMsgList.Count-1 do
                 begin
                   //do your work here
                   Writeln(AMsgList[i].Subject); //list the subject of the sent items
                 end;
            finally
              AMsgList.Free;
            end;
          finally
            LIdIMAP4.Disconnect;
          end;
        finally
          LIdSSLIOHandlerSocketOpenSSL.Free;
        end;
      finally
        LIdIMAP4.Free;
      end;
    end;
    
    
    
    begin
      try
       GetSentItems;
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
      readln;
    end.
    

    0 讨论(0)
提交回复
热议问题