加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 使用gmail和Indy发送电子邮件

发布时间:2020-12-15 09:23:15 所属栏目:大数据 来源:网络整理
导读:我正在尝试使用gmail从Delphi发送电子邮件. 我有Indy 10.5.9.0和Delphi XE3. 我从下面得到了示例代码: http://www.andrecelestino.com/delphi-xe-envio-de-e-mail-com-componentes-indy/ 我也尝试了其他示例代码,但结果相同. 我从这里有libeay32.dll和sslea
我正在尝试使用gmail从Delphi发送电子邮件.
我有Indy 10.5.9.0和Delphi XE3.

我从下面得到了示例代码:
http://www.andrecelestino.com/delphi-xe-envio-de-e-mail-com-componentes-indy/

我也尝试了其他示例代码,但结果相同.

我从这里有libeay32.dll和ssleay32.dll:
http://www.andrecelestino.com/wp-content/files/DLLs-SSL-DelphiXE.rar
但我也尝试过:
http://indy.fulgan.com/SSL/openssl-1.0.2d-i386-win32.zip
没有运气.

我的代码(全部):

uses
  Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls,ComCtrls,IdBaseComponent,IdComponent,IdTCPConnection,IdTCPClient,IdExplicitTLSClientServerBase,IdMessageClient,IdSMTPBase,IdMessage,IdSMTP,IdIOHandler,IdIOHandlerSocket,IdIOHandlerStack,IdSSL,IdSSLOpenSSL,IdText;






procedure TForm1.Button1Click(Sender: TObject);
var
  // variáveis e objetos necessários para o envio
  IdSSLIOHandlerSocket: TIdSSLIOHandlerSocketOpenSSL;
  IdSMTP: TIdSMTP;
  IdMessage: TIdMessage;
  IdText: TIdText;
  sAnexo: string;
begin
  // instancia??o dos objetos
  IdSSLIOHandlerSocket := TIdSSLIOHandlerSocketOpenSSL.Create(Self);
  IdSMTP := TIdSMTP.Create(Self);
  IdMessage := TIdMessage.Create(Self);

  try
    // Configura??o do protocolo SSL (TIdSSLIOHandlerSocketOpenSSL)
    IdSSLIOHandlerSocket.SSLOptions.Method := sslvSSLv23;
    IdSSLIOHandlerSocket.SSLOptions.Mode := sslmClient;

    // Configura??o do servidor SMTP (TIdSMTP)
    IdSMTP.IOHandler := IdSSLIOHandlerSocket;
    IdSMTP.UseTLS := utUseImplicitTLS;
    IdSMTP.AuthType := satDefault;
    IdSMTP.Port := 465;
    IdSMTP.Host := 'smtp.gmail.com';
    IdSMTP.Username := 'MYLOGIN@gmail.com';
    IdSMTP.Password := 'MYPASS';

    // Configura??o da mensagem (TIdMessage)
    IdMessage.From.Address := 'MYLOGIN@gmail.com';
    IdMessage.From.Name := 'John Smith';
    IdMessage.ReplyTo.EMailAddresses := IdMessage.From.Address;
    IdMessage.Recipients.Add.Text := 'receiver@example.com';
    IdMessage.Subject := 'Hello World';
    IdMessage.Encoding := meMIME;

    // Configura??o do corpo do email (TIdText)
    IdText := TIdText.Create(IdMessage.MessageParts);
    IdText.Body.Add('The body of the e-mail goes here');
    IdText.ContentType := 'text/plain; charset=iso-8859-1';


    // Conex?o e autentica??o
    try
      IdSMTP.Connect;
      IdSMTP.Authenticate;
    except
      on E:Exception do
      begin
        MessageDlg('Cannot authenticate: ' +
          E.Message,mtWarning,[mbOK],0);
        Exit;
      end;
    end;

    // Envio da mensagem
    try
      IdSMTP.Send(IdMessage);
      MessageDlg('Message sent successfully!',mtInformation,0);
    except
      On E:Exception do
      begin
        MessageDlg('Error while sending a message: ' +
          E.Message,0);
      end;
    end;
  finally
    // libera??o dos objetos da memória
    FreeAndNil(IdMessage);
    FreeAndNil(IdSSLIOHandlerSocket);
    FreeAndNil(IdSMTP);
  end;
end;

但我只从Gmail收到此错误:

https://accounts.google.com/ContinueSignIn?sarp=1&scc=1sdf[…]请通过网络浏览器登录
然后再试一次.
?了解更多信息
?https://support.google.com/mail/answer/78754 t1sm2526415lcc.25 – gsmtp

我登录https://accounts.google.com/ContinueSignIn?sarp=1&scc=1sdf[…],但我没有告诉我任何事情.

解决方法

这是因为Google以这种方式阻止了登录.
所以在花了10个小时三次检查我的代码中的每个可能设置后,我找到了这个页面:

https://www.google.com/settings/security/lesssecureapps

并启用“不太安全的应用程序”的访问权限.

更新:

之后尝试使用Delphi发送电子邮件,然后如果失败则登录到您的Gmail帐户并访问以下2个页面:

https://myaccount.google.com/device-activity

https://myaccount.google.com/secureaccount

并确认这个新的“未知设备”是你(这是你的Delphi应用程序).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读