Send e-mail to gmail with INDY - Delphi xe7 - delphi

IdMessage1.Clear;
IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlersocketopenSSL.Create(nil);
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvTLSv1;
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Mode := sslmUnassigned;
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.VerifyMode := [];
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.VerifyDepth := 0;
IdSSLIOHandlerSocketOpenSSL1.Host := 'smtp.gmail.com';
IdSSLIOHandlerSocketOpenSSL1.Port := 587;
IdSMTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
IdSMTP1.UseTLS := utUseExplicitTLS;
IdMessage1.Body.Append('h');
IdMessage1.From.Name := '******';
IdMessage1.From.Address := '****';
IdMessage1.Recipients.EMailAddresses :='*****';
IdMessage1.Subject := 'POWIADOMIENIE';
IdSMTP1.UserName := '*******';
IdSMTP1.Password := '*******';
IdSMTP1.Host :='smtp.gmail.com';
IdSMTP1.Port := 587 ;
//IdSMTP1.AuthType := satDefault;
// IdSMTP1.Authenticate;
IdSMTP1.Connect;
IdSMTP1.Send(IdMessage1);
IdSMTP1.Disconnect;
//idSMTP1.Authenticate;
end;
Hello, trying to send email to gmail using INDY and Delphi XE7 . Unfortunately, I get the error
"SSL in not availaible on this server "
screen from Wireshark

Changing libeay32.dll and ssleay32.dll to the current Version solves the problem!

Related

"SSL negotiation failed" in Delphi 11 while using office356 smtp server

I tried to send mail using office365 smtp server but i am getting error "SSL Negotiation Failed". I have the latest openssl dll files. So the host in that case is smtp.office365.com.
I also tried a code sample from other post but with no success.
Can you help me please ?
procedure TForm7.Button3Click(Sender: TObject);
var
IdSMTPa: TIdSMTP;
IdMessage1: TIdMessage;
IdSSL: TIdSSLIOHandlerSocketOpenSSL;
begin
IdSMTPa := TIdSMTP.Create(nil);
try
IdSMTPa.Host := 'smtp.office365.com';
IdSMTPa.Port := 587;
IdSMTPa.Username := 'mymail#mydomain.gr';
IdSMTPa.Password := 'mypwd';
IdSMTPa.AuthType := satDefault;
// IO HANDLER Settings //
IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(IdSMTPa);
IdSSL.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
IdSSL.SSLOptions.Mode := sslmUnassigned;
IdSSL.SSLOptions.VerifyMode := [];
IdSSL.SSLOptions.VerifyDepth := 0;
IdSMTPa.IOHandler := IdSSL;
IdSMTPa.UseTLS := utUseExplicitTLS;
IdMessage1 := TIdMessage.Create(nil);
try
IdMessage1.From.Address := 'mymail#mydomain.gr';
IdMessage1.Recipients.EMailAddresses := 'user1#mydomain.gr';
IdMessage1.CCList.EMailAddresses := 'user1#mydomain.gr';
IdMessage1.Subject := 'Test Email Subject';
IdMessage1.Body.Add('Test Email Body');
IdMessage1.Priority := mpHigh;
try
IdSMTPa.Connect();
try
IdSMTPa.Send(IdMessage1);
ShowMessage('Email sent');
finally
IdSMTPa.Disconnect();
end;
except
on e: Exception do
begin
ShowMessage('ERROR : '+e.Message);
end;
end;
finally
IdMessage1.Free;
end;
finally
IdSMTPa.Free;
end;
end;

error connecting with ssl delphi 2007 indy 10

I am new to Delphi and I am assigned with a Delphi 2007 desktop application and have been asked to enable https in that application current it uses Indy 10.1.5.
Below is the code which I have written but could not get expected output.
I am not sure if the DLLs that I use are compatible with it or not.
Error I get is "error connecting with ssl"
var
IdHTTP: TIdHTTP;
Id_HandlerSocket : TIdSSLIOHandlerSocketOpenSSL;
begin
IdHTTP := TIdHTTP.Create;
try
with IdHTTP do
begin
Name := 'IdHTTP';
AllowCookies := True;
RedirectMaximum := 25;
ProxyParams.BasicAuthentication := False;
ProxyParams.ProxyPort := 0;
Request.ContentLength := -1;
Request.Accept := 'text/html, */*';
Request.BasicAuthentication := False;
Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
HTTPOptions := [hoForceEncodeParams];
end;
Id_HandlerSocket := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
Id_HandlerSocket.SSLOptions.Method := sslvSSLv23;
Id_HandlerSocket.SSLOptions.Mode := sslmClient;
// Id_HandlerSocket.Port:= 443 ;
IdHTTP.IOHandler := Id_HandlerSocket;
try
Result := IdHTTP.Get('https://'+WebServer+'/myfol/test_file.txt'); // this is the place where I get error
except on E : Exception do
Begin
Result := E.Message +' || '+E.ClassName ;
End
end;
finally
IdHTTP.Free;
end;

How can I send email through google security?

I have developed an app for Android and iOS. I am using Delphi 10.2.3 Tokyo, with Indy 10.
I wrote a function for sending an email via Gmail with some help.
Although the function often works properly, sometimes it gets the error below when some participants log in Gmail with their own phones.
https://accounts.google.com/signIn/continue?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 y4-v6sm15938458pgy.18 - gsmtp
I have already seen Send e-mail using gmail and Indy, but I don't understand how I should use TIdSMTP.Password.
How should I revise my function to avoid this error?
The code is below.
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;

How can I send email over IPv6 properly?

I am developing an email sending function in my iOS and Android apps.
It is a function to send an email via Gmail using OpenSSL.
I am using Delphi 10.2.3 Tokyo, with Indy 10.
I submitted my iOS app to iTunes Connect, but they rejected my app because this function does not work in IPv6.
They said
We discovered one or more bugs in your app when reviewed on iPad and iPhone running iOS 11.4.1 on Wi-Fi connected to an IPv6 network.
They also send me a screenshot of the error saying
An error occurred when resolving address smtp.gmail.com: (8)
How can I fix this error to work with IPv6 properly? My code is below:
Procedure MailSend;
Var
Connected: Boolean;
Begin
IdSMTP := TIdSMTP.Create(nil);
try
IdSMTP.Host := 'smtp.gmail.com';
IdSMTP.Port := 587;
IdSMTP.Username := 'xxxx#gmail.com'; // UserName
IdSMTP.Password := 'xxxx'; // Password
SSL := TIdSSLIOHandlerSocketOpenSSL.Create;
try
SSL.Host := IdSMTP.Host;
SSL.Port := IdSMTP.Port;
SSL.Destination := SSL.Host + ':' + IntToStr(SSL.Port);
IdSMTP.IOHandler := SSL;
IdSMTP.UseTLS := utUseExplicitTLS;
IdSMTP.Socket.IPVersion := Id_IPv6;
try
IdSMTP.Connect;
Connected := True;
except
Connected := False;
end;
If Connected = False then
Begin
IdSMTP.Socket.IPVersion := Id_IPv4;
IdSMTP.Connect;
End;
Msg := TIdMessage.Create(IdSMTP);
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;
IdSMTP.Disconnect;
finally
SSL.Free;
end;
finally
IdSMTP.Free;
End;
End;
I see a few problems with your SMTP code:
you need to set the IdSMTP.IPVersion property instead of the IdSMTP.Socket.IPVersion property. The default value of the IPVersion property is Id_IPv4 (bug - it is not respecting the ID_DEFAULT_IP_VERSION constant in the IdGlobal unit). Connect() overwrites the Socket.IPVersion property value with the IPVersion property value, so you are actually attempting to connect using Id_IPv4 twice, which will fail on an IPv6-only network (which Apple requires apps to support).
you are not catching any errors from the 2nd Connect(). That is likely the error that Apple is ultimately seeing.
you should not be setting the SSL.Host, SSL.Port, and SSL.Destination properties manually. Let Connect() handle that for you.
Try this instead:
// this accessor class is needed because TIdSMTP derives from TIdTCPClientCustom
// instead of TIdTCPClient. The IPVersion property is protected in
// TIdTCPClientCustom and not published by TIdSMTP or its ancestors.
//
// See https://github.com/IndySockets/Indy/issues/184 ...
//
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;
Alternatively:
type
TIdSMTPAccess = class(TIdSMTP)
end;
procedure MailSend;
var
IdSMTP: TIdSMTP;
Msg: TIdMessage;
Connected: Boolean;
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;
Connected := False;
if GStack.SupportsIPv6 then
begin
TIdSMTPAccess(IdSMTP).IPVersion := Id_IPv6;
try
IdSMTP.Connect;
Connected := True;
except
end;
end;
if (not Connected) and GStack.SupportsIPv4 then
begin
TIdSMTPAccess(IdSMTP).IPVersion := Id_IPv4;
try
IdSMTP.Connect;
Connected := True;
except
end;
end;
if not Connected then
begin
// unable to connect!
Exit;
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;

Indy SMTP sending email with GMail suddenly stopped working

Using: Delphi XE7, latest daily build of Indy 10.
This code was working 2 months earlier. Now I recompiled my app, and its not working anymore, with the same account details. The exception message is 'Disconnected'.
The SMTP port used is 587.
Code follows:
FIdSMTP := TIdSMTP.Create;
FIdSSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
FIdMessage := TIdMessage.Create;
FIdAttachment := TIdAttachmentFile.Create(FIdMessage.MessageParts);
if (FEmailServer.SMTPUseSSL) then
begin
FIdSSLIOHandler.SSLOptions.Method := sslvTLSv1;
// FIdSSLIOHandler.SSLOptions.Method := sslvSSLv3;
FIdSMTP.IOHandler := FIdSSLIOHandler;
FIdSMTP.UseTLS := utUseExplicitTLS;
// FIdSMTP.UseTLS := utUseImplicitTLS;
end;
FIdSMTP.Host := FEmailServer.SMTPHost; //smtp.gmail.com
FIdSMTP.Port := FEmailServer.SMTPPort; //587
FIdSMTP.AuthType := satDefault;
FIdSMTP.Username := FEmailServer.SMTPUserName;
FIdSMTP.Password := FEmailServer.SMTPPassword;
FIdMessage.Recipients[0].Address := mailmessage.RecipientEmailAddr;
FIdMessage.From.Name := mailmessage.SenderName;
FIdMessage.Subject := mailmessage.EmailSubject;
FIdMessage.Body.Text := mailmessage.EmailMessage;
FIdAttachment.LoadFromFile(mailmessage.AttachmentFile);
FIdAttachment.FileName := ExtractFileName(mailmessage.AttachmentFile);
try
if (not FIdSMTP.Connected) then
FIdSMTP.Connect;
FIdSMTP.Send(FIdMessage);
except
on E: Exception do
ShowMessage(E.Message);
end;
Do you see anything obvious that could be causing it to not send the email?
Thank you, Remy! Beautiful tip! This code now works:
FIdSMTP := TIdSMTP.Create;
FIdSSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
FIdMessage := TIdMessage.Create;
FIdAttachment := TIdAttachmentFile.Create(FIdMessage.MessageParts);
FIdSASLCRAMSHA1 := TIdSASLCRAMSHA1.Create;
FIdSASLCRAMMD5 := TIdSASLCRAMMD5.Create;
FIdSASLDigest := TIdSASLDigest.Create;
FIdSASLSKey := TIdSASLSKey.Create;
FIdSASLOTP := TIdSASLOTP.Create;
FIdSASLAnonymous := TIdSASLAnonymous.Create;
FIdSASLExternal := TIdSASLExternal.Create;
FIdSASLLogin := TIdSASLLogin.Create;
FIdSASLPlain := TIdSASLPlain.Create;
if (FEmailServer.SMTPUseSSL) then
begin
FIdSSLIOHandler.SSLOptions.Method := sslvTLSv1;
// FIdSSLIOHandler.SSLOptions.Method := sslvSSLv3;
FIdSMTP.IOHandler := FIdSSLIOHandler;
FIdSMTP.UseTLS := utUseExplicitTLS;
// FIdSMTP.UseTLS := utUseImplicitTLS;
end;
FIdSMTP.Host := FEmailServer.SMTPHost; //smtp.gmail.com
FIdSMTP.Port := FEmailServer.SMTPPort; //587
FIdSMTP.AuthType := satSASL;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLCRAMSHA1;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLCRAMMD5;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLDigest;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLSKey;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLOTP;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLAnonymous;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLExternal;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLLogin;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLPlain;
FIdSMTP.Username := FEmailServer.SMTPUserName;
FIdSMTP.Password := FEmailServer.SMTPPassword;
FIdMessage.Recipients[0].Address := mailmessage.RecipientEmailAddr;
FIdMessage.From.Name := mailmessage.SenderName;
FIdMessage.Subject := mailmessage.EmailSubject;
FIdMessage.Body.Text := mailmessage.EmailMessage;
FIdAttachment.LoadFromFile(mailmessage.AttachmentFile);
FIdAttachment.FileName := ExtractFileName(mailmessage.AttachmentFile);
try
if (not FIdSMTP.Connected) then
FIdSMTP.Connect;
FIdSMTP.Send(FIdMessage);
except
on E: Exception do
ShowMessage(E.Message);
end;
Freeing up the objects:
FIdAttachment.Free;
FIdMessage.Free;
FIdSSLIOHandler.Free;
FIdSMTP.Free;
FIdSASLCRAMSHA1.Free;
FIdSASLCRAMMD5.Free;
FIdSASLDigest.Free;
FIdSASLSKey.Free;;
FIdSASLOTP.Free;
FIdSASLAnonymous.Free;
FIdSASLExternal.Free;
FIdSASLLogin.Free;
FIdSASLPlain.Free;
..the USES list:
uses Classes, SysUtils, Windows, IdSMTP, IdMessage, IdSSLOpenSSL,
IdExplicitTLSClientServerBase, IdAttachmentFile, IdSASL,
IdSASLCollection, IdSASLAnonymous, IdSASLDigest, IdSASLExternal,
IdSASLLogin, IdSASLOTP, IdSASLPlain, IdSASLSKey, IdSASLUserPass,
IdSASL_CRAM_MD5, IdSASL_CRAM_SHA1;

Resources