Delphi - Indy - saving GMail draft

2019-04-29 00:35发布

I've been using Indy under Delphi to send message via a gmail account, using the TIdSMTP and TIdMessage components. This works absolutely fine.

However, my client has requested to save the message to the DRAFTS folder to allow him to make changes to the (programatically created) message before actually sending it.

GMail API is supposed to support this, but the provided examples are not in Delphi/Indy formats... I'm looking for minimal programming changes so I'd like to know if it is possible in the Indy components too? TIdMessage allows for a "mfDraft" flag, but this doesn't prevent the message from immediatly being sent when using IdSMTP1.Send

2条回答
小情绪 Triste *
2楼-- · 2019-04-29 01:37

SMTP has no concept of drafts. You have to use IMAP instead.

Use TIdIMAP4 to login to the GMail account, call its SelectMailBox() method to select the draft folder, and then call one of its AppendMsg...() methods to store a draft of the email into the folder as needed.

If you want to make changes to a draft before sending it, you will have to retrieve the current draft from the folder (one of the Retrieve...() or UIDRetrieve...() methods) and edit is as needed, then delete the current draft from the folder (the DeleteMsgs() or UIDDeleteMsg() method), and append the new draft into the folder.

To actually send a draft, you will have to retrieve and remove it from the draft folder, and then use SMTP to send it.

查看更多
可以哭但决不认输i
3楼-- · 2019-04-29 01:40

OK, for anyone reading this thread in the future... here's the code needed: (gebr and ww are Username and password)

procedure DraftGMail(Info:TIdMessage; gebr,ww:string);
var


  IdIMAP41:TIdIMAP4;
  IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
begin

  IdSSLIOHandlerSocketOpenSSL1:= TIdSSLIOHandlerSocketOpenSSL.create;
  try
    IdSSLIOHandlerSocketOpenSSL1.Destination := 'imap.gmail.com:993';
    IdSSLIOHandlerSocketOpenSSL1.host := 'imap.gmail.com';
//    IdSSLIOHandlerSocketOpenSSL1.MaxLineAction := maException;
    IdSSLIOHandlerSocketOpenSSL1.Port := 993;
    IdSSLIOHandlerSocketOpenSSL1.DefaultPort := 0;
    IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvSSLv3;
    IdSSLIOHandlerSocketOpenSSL1.SSLOptions.SSLversions := [sslvSSLv3];
    IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Mode := sslmUnassigned;
    IdSSLIOHandlerSocketOpenSSL1.SSLOptions.VerifyMode := [];
    IdSSLIOHandlerSocketOpenSSL1.SSLOptions.VerifyDepth := 0;

    IdIMAP41:= TIdIMAP4.create;
    try
      IdIMAP41.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
      IdIMAP41.Host := 'imap.gmail.com';
      IdIMAP41.Password := ww;
      IdIMAP41.Port := 993;
//      IdSMTP1.SASLMechanisms := <>;
      IdIMAP41.UseTLS := utUseImplicitTLS;
      IdIMAP41.Username := gebr;

        IdIMAP41.Connect;
//        IdIMAP41.ListMailBoxes(Boxes);
        IdIMAP41.SelectMailbox('[Gmail]/Drafts');
        IdIMAP41.AppendMsg('[Gmail]/Drafts',Info);
        IdIMAP41.Disconnect;

    finally
      FreeAndNil(IdIMAP41);
    end;

  finally
    FreeAndNil(IdSSLIOHandlerSocketOpenSSL1);

  end;
end;
查看更多
登录 后发表回答