使用gmail和Indy发送电子邮件 [英] Send e-mail using gmail and Indy
问题描述
我正在尝试使用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://acrefers.google .com/ContinueSignIn?sarp = 1& scc = 1sdf [...] ,但我没有真正告诉我任何事情.
这是因为Google以这种方式阻止了登录. 因此,在花了10个小时仔细检查了我代码中的所有可能设置之后,我找到了此页面:
https://www.google.com/settings/security/lesssecureapps
并启用了对安全性较低的应用程序"的访问.
更新:
然后尝试使用Delphi发送电子邮件,如果失败,请登录您的Gmail帐户并访问以下两个页面:
https://myaccount.google.com/device-activity
https://myaccount.google.com/secureaccount
并确认您是这个新的未知设备"(这是您的Delphi应用程序).
I am trying to send an e-mail from Delphi using gmail. I have Indy 10.5.9.0 and Delphi XE3.
I got example code from: http://www.andrecelestino.com/delphi-xe-envio-de-e-mail-com-componentes-indy/
I also tried other example codes but with same results.
I have libeay32.dll and ssleay32.dll from here: http://www.andrecelestino.com/wp-content/files/DLLs-SSL-DelphiXE.rar but I also tried: http://indy.fulgan.com/SSL/openssl-1.0.2d-i386-win32.zip with no luck.
My code (FULL):
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;
But I only get this error from Gmail:
https://accounts.google.com/ContinueSignIn?sarp=1&scc=1sdf[...] Please log in via your web browser and then try again. Learn more at https://support.google.com/mail/answer/78754 t1sm2526415lcc.25 - gsmtp
I did log on https://accounts.google.com/ContinueSignIn?sarp=1&scc=1sdf[...] but I didn't tell me anything really.
It was because Google blocked login this way. So after spending 10 hours triple checking every possible setting in my code I found this page:
https://www.google.com/settings/security/lesssecureapps
and enabled access for "less secure apps".
UPDATE:
After that try to send an e-mail with Delphi and then if it fails log into your Gmail account and visit these 2 pages:
https://myaccount.google.com/device-activity
https://myaccount.google.com/secureaccount
and confirm this new "unknown device" is you (it's your Delphi app).
这篇关于使用gmail和Indy发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!