首页 > 解决方案 > 如何通过谷歌安全发送电子邮件?

问题描述

我已经为 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;

标签: delphismtpgmail

解决方案


推荐阅读