Android 如何使用OAuth2.0从印地发送Gmail?

Android 如何使用OAuth2.0从印地发送Gmail?,android,delphi,oauth,indy10,Android,Delphi,Oauth,Indy10,下面的代码使用Google的Gmail服务器成功地发送了一封电子邮件,但只有在降低Google帐户安全设置以“允许不太安全的应用程序”之后 下面提供的代码(最初来自Remy LeBeau)不包含OAuth 2.0,如果您不想要求用户做出看似艰难的决定,降低其安全设置以允许应用程序成功,则需要使用OAuth 2.0。如何将OAuth2.0整合到Indy解决方案中,以满足Google更高的安全标准 工作解决方案: function TTabbedwithNavigationForm.SendEmai

下面的代码使用Google的Gmail服务器成功地发送了一封电子邮件,但只有在降低Google帐户安全设置以“允许不太安全的应用程序”之后

下面提供的代码(最初来自Remy LeBeau)不包含OAuth 2.0,如果您不想要求用户做出看似艰难的决定,降低其安全设置以允许应用程序成功,则需要使用OAuth 2.0。如何将OAuth2.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;  // 'username@gmail.com';
  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;
函数TTabbedwithNavigationForm.SendEmailNow(FromStr、ToStr、Subject、,
MessageBody,Host:String;Port:Integer;UserName,Pass:String):布尔值;
开始
///从Remy LeBeau Indy通过gmail主机通过SSL发送SMTP
结果:=假;
尝试
IdMessage1:=nil;
IDssSlioHandlerSocketOpenSSL1:=零;
IdSMTP1:=nil;
尝试
//设置邮件消息
尝试
IdMessage1:=TIdMessage.Create(无);
IdMessage1.From.Address:=FromStr;///更改为联盟电子邮件
IdMessage1.Recipients.EMailAddresses:=ToStr;
IdMessage1.主题:=主题;
IdMessage1.Body.Text:=MessageBody;
//如果文件存在(datafilename),则
//IdAttachmentFile:=TIdAttachmentFile.Create(IdMessage1.MessageParts,datafilename);
除了
Exception.RaiseOuterException(Exception.Create('无法创建消息,请稍后再试');
结束;
//设置TLS
尝试
IdSSLIOHandlerSocketOpenSSL1:=TIdSSLIOHandlersocketopenSSL.Create(nil);
idsslohandlersocketopenssl1.SSLOptions.Method:=sslvTLSv1;
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Mode:=sslUnassigned;
IDS SLIOHandlerSocketOpenSSL1.SSLOptions.VerifyMode:=[];
IDS SLIOHandlerSocketOpenSSL1.SSLOptions.VerifyDepth:=0;
除了
Exception.RaiseOuterException(Exception.Create('无法创建SSL处理程序,请稍后重试');
完尝试ssl
//设置SMTP
尝试
IdSMTP1:=tidstp.Create(无);
IdSMTP1.IOHandler:=IdSSLIOHandlerSocketOpenSSL1;
IdSMTP1.UseTLS:=utUseExplicitTLS;
IdSMTP1.Host:=主机;/'smtp.gmail.com",;
IdSMTP1.端口:=端口//587;
IdSMTP1.Username:=用户名;/'username@gmail.com';
IdSMTP1.password:=通过//***gmail帐户密码';
除了
Exception.RaiseOuterException(Exception.Create('无法创建SMTP处理程序,请稍后再试');
完尝试
尝试
IdSMTP1.Connect;
尝试
IdSMTP1.Send(IdMessage1);
最后
IdSMTP1.断开连接;
结束;
除了
Exception.RaiseOuterException(Exception.Create('无法发送安全电子邮件,请稍后再试');
结束;
最后
IdSMTP1.免费;
IdSSLIOHandlerSocketOpenSSL1.免费;
IdMessage1.免费;
结果:=真;
结束;
除了
关于E:Exception-do
开始
如果E.InnerException为零,则
ShowMessage('错误:'+E.Message+#13#13+E.InnerException.Message)
其他的
ShowMessage('错误:'+E.Message);
结束;
结束;
///结束Remy-LeBeau代码
结束;

您需要导入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();

你检查过了吗?我的Gmail帐户启用了两步验证(禁用了“允许不太安全的应用”选项),我可以用Indy使用Google应用密码而不是OAuth登录Gmail。嘿,我已经在和原始解决方案的作者交谈了——非常酷。在我的应用程序成功之前,我不敢依赖任何需要用户更改其谷歌帐户设置的解决方案。如果向我的Delphi XE5移动应用程序添加Google应用程序密码意味着用户不必对其Google帐户进行任何更改,那么这将是一个很好的解决方案。您能解释一下如何将Google应用程序密码添加到Delphi项目或SMTP组件属性中吗?您不会将密码添加到项目ImageBase中。相反,您将一如既往地继续请求用户的密码。但是,用户提供的不是常规密码,而是用户需要的特定于应用程序的密码。例如,我的旧Android手机有一个特定于应用程序的密码,因为它的帐户设置早于谷歌的两步OAuth机制。Leonardo Herrera的评论对于更好地理解将OAuth 2与Indy组件结合所需的工作非常有帮助。不幸的是,演示的组件是IMAP,而不是SMTP。我的要求是发送电子邮件,而不是从帐户的收件箱中获取/阅读电子邮件。