通过Gmail发送电子邮件,并使用应用程序特定密码发送电子邮件
我维护和开发一个程序(除其他方面)通过Gmail发送电子邮件。
到目前为止,发送电子邮件的发送还没有问题,但是几天前,这种功能停止使用消息“不良凭据”。我仔细研究了Gmail的帮助,发现了这种解释/警告/什么是您:
从2022年5月30日开始,Google不再支持使用第三方应用程序或设备的使用,这些应用程序要求您仅使用用户名和密码登录Google帐户。
解决方案是使用“特定应用”密码。
我看着这个问题 :
我最近的尝试是在gmail上创建一个“特定应用”密码
,但附件的代码实际上并未显示如何发送密码。
阅读问题及其答案,我对程序进行了一些更改:
Port := 995
UseTLS := utUseImplicitTLS
SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2]
这些更改后,尝试发送测试邮件(仍然没有应用程序密码),我得到答复
回复代码无效: +ok
可能意味着我的程序失败,因为它不会发送特定于应用程序的密码。
我想知道的是:如何发送该密码?
以下是我的代码,该代码是作为线程执行的,因此所有组件均在代码中定义:
email:= TIdMessage.Create (nil);
try
email.LoadFromFile (FFileName);
email.OnInitializeISO:= DoInitializeISO;
// DeleteFile (FFileName);
smtp:= TIdSMTP.Create (nil);
try
smtp.OnFailedRecipient:= FailedRecipient;
ssl:= TIdSSLIOHandlerSocketOpenSSL.Create (smtp);
ssl.SSLOptions.SSLVersions:= [sslvTLSv1];
smtp.OnStatus := DoStatus;
smtp.IOHandler:= ssl;
smtp.Host:= FHost;
smtp.Password:= FPassword; // now app passord
smtp.Username:= FUsername;
smtp.UseTLS:= utUseExplicitTLS;
smtp.Port:= 587;
smtp.Connect;
try
smtp.Send (email);
finally
smtp.Disconnect;
end;
finally
ssl.free;
smtp.Free;
end;
finally
email.Free;
end;
I maintain and develop a program that (amongst other things) sends emails via GMail.
Until now, there have been no problems with sending emails, but a few days ago this functionality ceased to work with a message 'Bad credentials'. I looked through the help of GMail and found this explanation/warning/what-have-you:
To help keep your account secure, from May 30, 2022, Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.
The solution is to use the 'app specific' password.
I looked at this question that states:
My most recent try was to create an 'app specific' password on Gmail
But the attached code doesn't actually show how the password is sent.
Reading the question and its answers, I made some changes to my program:
Port := 995
UseTLS := utUseImplicitTLS
SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2]
After these changes, in an attempt to send a test mail (still no app specific password), I get the response
Reply code is not valid: +OK
which might mean that my program fails, as it doesn't send the app specific password.
What I want to know is: how to send that password?
Below is my code that executes as a thread, hence all components are defined in code:
email:= TIdMessage.Create (nil);
try
email.LoadFromFile (FFileName);
email.OnInitializeISO:= DoInitializeISO;
// DeleteFile (FFileName);
smtp:= TIdSMTP.Create (nil);
try
smtp.OnFailedRecipient:= FailedRecipient;
ssl:= TIdSSLIOHandlerSocketOpenSSL.Create (smtp);
ssl.SSLOptions.SSLVersions:= [sslvTLSv1];
smtp.OnStatus := DoStatus;
smtp.IOHandler:= ssl;
smtp.Host:= FHost;
smtp.Password:= FPassword; // now app passord
smtp.Username:= FUsername;
smtp.UseTLS:= utUseExplicitTLS;
smtp.Port:= 587;
smtp.Connect;
try
smtp.Send (email);
finally
smtp.Disconnect;
end;
finally
ssl.free;
smtp.Free;
end;
finally
email.Free;
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
特定于应用程序的密码只是Gmail为您生成的密码,然后您使用它而不是普通密码(即,在
tidsmtp.password Wordword
属性中)。这在Gmail的文档中说明了:An application-specific password is simply a password that Gmail generates for you, and then you use it instead of your normal password (ie, in the
TIdSMTP.Password
property). This is explained in Gmail's documentation:Sign in with App Passwords
您只需用应用程序密码替换普通密码即可。假设您的代码类似于下面的代码。请记住,您需要启用2FA才能创建应用程序密码。
另一个选择是使用XOAuth2。
You can just replace the normal password with the app password. Assuming your code is similar to that below. Just remember you need to have 2FA enabled in order to create an app password.
Another option would be to use XOAuth2.