1-first login to : https://www.google.com/settings/security/lesssecureapps and turn on access for secure apps
2-Create on form : button, IdSSLIOHandlerSocketOpenSSL1, IdMessage1, IdSMTP1
3-Code
uses
idglobal;
procedure SendEmail(const Recipients: string; const Subject: string; const Body: string);
var
SMTP: TIdSMTP;
Email: TIdMessage;
SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
eFrom: string;
begin
eFrom := 'from@gmail.com';
SMTP := TIdSMTP.Create(nil);
Email := TIdMessage.Create(nil);
SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
SSLHandler.MaxLineAction := maException;
SSLHandler.SSLOptions.Method := sslvTLSv1;
SSLHandler.SSLOptions.Mode := sslmUnassigned;
SSLHandler.SSLOptions.VerifyMode := [];
SSLHandler.SSLOptions.VerifyDepth := 0;
SSLHandler.Host := 'smtp.gmail.com';
SSLHandler.Port := 587;
SMTP.IOHandler := SSLHandler;
SMTP.Host := 'smtp.gmail.com';
SMTP.Port := 587;
SMTP.Username := eFrom;
SMTP.Password := 'xxxxxx';
SMTP.UseTLS := utUseExplicitTLS;
Email.From.Address := eFrom;
Email.Recipients.EmailAddresses := Recipients;
Email.Subject := Subject;
Email.Body.Text := Body;
SMTP.Connect; //always raise 'Host not found'
SMTP.Send(Email);
SMTP.Disconnect;
finally
SMTP.Free;
Email.Free;
SSLHandler.Free;
end;
end;
;------------------------------------------------------------------------
procedure TestSendMail(Sender: TObject);
begin
SendEmail('to@yahoo.com', 'Subject', 'Body');
end;
No comments:
Post a Comment