gpt4 book ai didi

android - 如何使用 OAuth 2.0 从 Indy 发送 Gmail?

转载 作者:可可西里 更新时间:2023-11-01 19:08:31 43 4
gpt4 key购买 nike

以下代码使用 Google 的 Gmail 服务器成功发送电子邮件,但前提是将 Google 帐户安全设置降低为“允许安全性较低的应用”。

下面提供的代码(最初来自 Remy LeBeau)没有包含 OAuth 2.0,如果您不想让您的用户做出看似艰难的决定来降低他们的安全设置以使您的应用程序成功,则需要 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; // '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;

最佳答案

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

关于android - 如何使用 OAuth 2.0 从 Indy 发送 Gmail?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26741924/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com