如何通过Google Security发送电子邮件?

时间:2018-08-09 12:30:22

标签: delphi smtp gmail

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

我已经看过Send e-mail using gmail and 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;

0 个答案:

没有答案
相关问题