gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:52 24 4
gpt4 key购买 nike

我正在尝试使用 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, [mbOK], 0);
except
On E:Exception do
begin
MessageDlg('Error while sending a message: ' +
E.Message, mtWarning, [mbOK], 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 应用程序)。

关于delphi - 使用 gmail 和 Indy 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31776715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com