2014-11-04 75 views
11

以下代碼使用Google的Gmail服務器成功發送電子郵件,但只有在將Google帳戶安全設置降低爲「允許安全性較低的應用程序」後才能成功發送。如何使用OAuth 2.0從Indy發送Gmail?

下面提供的代碼(最初來自Remy LeBeau)未包含OAuth 2.0,如果您不想要求用戶做出看似艱難的決定來降低其安全設置以允許您的應用程序成功,那麼這是必需的。如何將OAuth 2.0納入Indy解決方案以滿足Google更高的安全標準?

工作液:

function TTabbedwithNavigationForm.SendEmailNow(FromStr, ToStr, Subject, 
MessageBody, Host: String; Port: Integer; UserName, Pass: String): Boolean; 
    begin 

///From Remy LeBeau Indy SMTP with SSL via gmail host 
Result := False; 

try 
    IdMessage1 := nil; 
    IdSSLIOHandlerSocketOpenSSL1 := nil; 
    IdSMTP1 := nil; 
    try 
    //setup mail message 
    try 
     IdMessage1       := TIdMessage.Create(nil); 
     IdMessage1.From.Address    := FromStr;//// change to league email 
     IdMessage1.Recipients.EMailAddresses := ToStr; 
     IdMessage1.Subject      := Subject; 
     IdMessage1.Body.Text     := MessageBody; 
     //if FileExists(datafilename) then 
     // IdAttachmentFile := TIdAttachmentFile.Create(IdMessage1.MessageParts, datafilename); 
    except 
     Exception.RaiseOuterException(Exception.Create('Could not create message, please try again later')); 
    end; 

//setup TLS 
try 
    IdSSLIOHandlerSocketOpenSSL1     := TIdSSLIOHandlersocketopenSSL.Create(nil); 
    IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method  := sslvTLSv1; 
    IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Mode  := sslmUnassigned; 
    IdSSLIOHandlerSocketOpenSSL1.SSLOptions.VerifyMode := []; 
    IdSSLIOHandlerSocketOpenSSL1.SSLOptions.VerifyDepth := 0; 
except 
    Exception.RaiseOuterException(Exception.Create('Could not create SSL handler, please try again later')); 
end; // of try ssl 

//setup SMTP 
try 
    IdSMTP1   := TIdSMTP.Create(nil); 
    IdSMTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1; 
    IdSMTP1.UseTLS := utUseExplicitTLS; 
    IdSMTP1.Host  := Host;//'smtp.gmail.com'; 
    IdSMTP1.Port  := Port;//587; 
    IdSMTP1.Username := UserName; // '[email protected]'; 
    IdSMTP1.password := Pass; //***gmail account password'; 
except 
    Exception.RaiseOuterException(Exception.Create('Could not create SMTP handler, please try again later')); 
end; // of try 

try 
    IdSMTP1.Connect; 
    try 
    IdSMTP1.Send(IdMessage1) ; 
    finally 
    IdSMTP1.Disconnect; 
    end; 
except 
    Exception.RaiseOuterException(Exception.Create('Could not send secure email, please try again later')); 
end; 
    finally 
    IdSMTP1.Free; 
    IdSSLIOHandlerSocketOpenSSL1.Free; 
    IdMessage1.Free; 
    Result := True; 
    end; 
except 
    on E: Exception do 
    begin 
    if E.InnerException <> nil then 
     ShowMessage('ERROR: ' + E.Message + #13#13 + E.InnerException.Message) 
    else 
     ShowMessage('ERROR: ' + E.Message); 
    end; 
end; 

/// End Remy LeBeau Code 

end; 
+4

你檢查[此線索](https://forums.embarcadero.com/thread.jspa?threadID= 107724&tstart = 975)和[這個項目](https://github.com/lordcrc/IndySASLOAuth2)? – 2014-11-04 18:17:50

+1

我的Gmail帳戶啓用了兩步驗證功能(禁用「允許安全性較低的應用」選項),我可以使用Google應用密碼而不是OAuth通過Indy登錄Gmail。 – 2014-11-04 20:57:10

+0

嘿,我已經和原來的解決方案的作者談過了 - 非常酷。我很猶豫依靠任何需要用戶更改其Google帳戶設置的解決方案 - 在我的應用程序成功之前。如果將Google應用程序密碼添加到我的Delphi XE5移動應用程序意味着用戶不必對他們的Google帳戶進行任何更改 - 那麼這將是一個很好的解決方案。您能否解釋如何將Google應用程序密碼添加到Delphi項目或SMTP組件屬性中? – ImageBASE 2014-11-04 21:16:22

回答

-3

您需要導入的lib BackgroundMailLibrary

BackgroundMail bm = new BackgroundMail(PasswordChangeActivity.this); 
              bm.setGmailUserName(mail id); 
              bm.setGmailPassword(Utils.decryptIt(password)); 
              bm.setMailTo(ownerEmail); 
              bm.setFormSubject(subject); 
              bm.setFormBody(body); 
              bm.send();