1

我的程序通过 GMail 向联系人发送电子邮件。通常这很有效,但我们注意到有时我的程序“认为”它发送的电子邮件实际上并没有到达 Gmail,更不用说到达联系人了。我认为我可以在程序中添加一个检查,该检查访问 Gmail“已发送邮件”文件夹以查看每封电子邮件是否确实已发送。

我有一些使用 TIdPOP3 组件的代码,但这会从收件箱中下载标题,而不是从已发送的项目中下载。我的问题是,如何访问已发送邮件文件夹中的标题?

以下是我正在使用的代码。它只是测试代码,所以没有任何 try/finally 块等。

 with pop do
  begin
   host:= 'pop.gmail.com';
   username:= 'someone@gmail.com';
   password:= .....;
   Port:= 995;
   Connect;
   if connected then 
    with i:= checkmessages downto 1 do
     begin
      msg.clear;   // msg is of type TIdMessage
      if retrieve (i, msg)
       then listbox1.items.add (msg.subject)
     end;
   disconnect
  end;
4

1 回答 1

3

要获取与已发送项目相关的信息,您可以使用Gmail imap_extensionsTIdIMAP4组件。

试试这个样本

{$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.
于 2013-06-29T06:16:20.950 回答