delphi - 如何通过谷歌安全发送电子邮件?
问题描述
我已经为 Android 和 iOS 开发了一个应用程序。我正在使用带有 Indy 10 的 Delphi 10.2.3 Tokyo。
在一些帮助下,我编写了一个通过 Gmail 发送电子邮件的函数。
虽然该功能通常可以正常工作,但有时在部分参与者使用自己的手机登录 Gmail 时会出现以下错误。
https://accounts.google.com/signIn/continue?sarp=1&scc=1sdf[...]请通过您的网络浏览器登录,然后重试。在https://support.google.com/mail/answer/78754 y4-v6sm15938458pgy.18 - gsmtp了解更多信息
我已经看过使用 gmail 和 Indy 发送电子邮件,但我不明白应该如何使用TIdSMTP.Password
。
我应该如何修改我的函数以避免这个错误?
代码如下。
type
TIdSMTPAccess = class(TIdSMTP)
end;
procedure MailSend;
var
IdSMTP: TIdSMTP;
Msg: TIdMessage;
begin
IdSMTP := TIdSMTP.Create(nil);
try
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(IdSMTP);
IdSMTP.IOHandler := SSL;
IdSMTP.Host := 'smtp.gmail.com';
IdSMTP.Port := 587;
IdSMTP.Username := 'xxxx@gmail.com';
IdSMTP.Password := 'xxxx';
IdSMTP.UseTLS := utUseExplicitTLS;
TIdSMTPAccess(IdSMTP).IPVersion := Id_IPv6;
try
IdSMTP.Connect;
except
TIdSMTPAccess(IdSMTP).IPVersion := Id_IPv4;
try
IdSMTP.Connect;
except
// unable to connect!
Exit;
end;
end;
try
Msg := TIdMessage.Create(nil);
try
Msg.OnInitializeISO := IdMessage_InitializeISO;
Msg.ContentType := 'text/plain';
Msg.CharSet := 'UTF-8';
Msg.ContentTransferEncoding := 'BASE64'; // BASE64 (7bit)
//Msg.ContentTransferEncoding := '8bit'; // RAW(8bit)
Msg.From.Name := SsNoSt;
Msg.From.Address := 'xxxx@gmail.com';
Msg.Recipients.EMailAddresses := 'xxxx@gmail.com';
Msg.Subject := SsNoSt;
Msg.Body.Text := 'Unicode String (body)';
IdSMTP.Send(Msg);
finally
Msg.Free;
end;
finally
IdSMTP.Disconnect;
end;
finally
IdSMTP.Free;
end;
end;
解决方案
推荐阅读
- javascript - aframe 创建复杂元素
- python - 使用熊猫数据框后无法将字符串转换为浮点数
- html - 使用 Css calc() 对齐伪元素以在所有屏幕上工作
- python - Django 2.1.7,IntegrityError,外键约束失败
- php - 在 laravel 中间件上调用 null 上的成员函数 setCookie()
- charles-proxy - 无法将三星 S10 连接到查尔斯代理
- chart.js - 使chartjs图例标签可标签化
- python - 熊猫指定自定义假期
- javascript - 将两个数组转换为 JSON 对象
- javascript - 服务器上的推送通知问题