Recent

Author Topic: [SOLVED] Synapse smtp.gmail.com GMail SSL authentication problem  (Read 5598 times)

patyi

  • Full Member
  • ***
  • Posts: 168
[SOLVED] Synapse smtp.gmail.com GMail SSL authentication problem
« on: February 26, 2018, 12:57:32 pm »
Hi all !

I'm modified some code based on http://forum.lazarus.freepascal.org/index.php?topic=27222.0, trying to send e-mail via smtp.gmail,com,
and have problem with SSL or TLS authentication.

Usage of my function :
Code: Pascal  [Select][+][-]
  1. uses ..., SMTPSend, MIMEPart, MIMEMess;
  2. ...
  3.  if FDataMod.SendEMail('sender@gmail.com', 'reciver@eunet.rs',
  4.                               'Subject', 'Body text TStrings', 'Attachments TStrings',
  5.                               'Username', 'password',
  6.                               'smtp.gmail.com', '465', 'SSL') then
  7.           ShowMessage('Sending OK !')
  8.         else
  9.           ShowMessage('Sending failed  !');  
  10. ...
  11.  

Source of my function :
Code: Pascal  [Select][+][-]
  1. function TFDataMod.SendEMail(const snd, rcv, sub: string;
  2.                              const msg, att: TStrings;
  3.                              const use, pas, hos, por, tsl: string): boolean;
  4. var
  5.   M : TMimeMess;
  6.   P : TMimePart;
  7.   i : integer;
  8.   po, ss : string;
  9. begin
  10.   M := TMimeMess.Create;
  11.   try
  12.     M.Header.From := snd;
  13.     M.Header.ToList.CommaText := rcv;
  14.     M.header.Subject := sub;
  15.     P := M.AddPartMultipart('mixed', nil);
  16.     M.AddPartText(msg, P);
  17.     for i := 0 to att.Count-1 do
  18.       M.AddPartBinaryFromFile(att.Strings , P);
  19.     M.EncodeMessage;
  20.  
  21.     po := '';
  22.     if por > '' then
  23.       po := ':'+por;
  24.  
  25.     ss := '';
  26.     if (Pos('TLS', tsl) > 0) or (Pos('SSL', tsl) > 0) then
  27.       ss := '&'+tsl;
  28.  
  29.     Result := SendToRaw(snd, rcv, hos+po+ss, M.Lines, use, pas);
  30.   finally
  31.     M.Free;
  32.   end;
  33. end;
  34.  

The modified Synapde smtpsend.pas unit, SendToRaw function
Code: Pascal  [Select][+][-]
  1. function SendToRaw(const MailFrom, MailTo, SMTPHost: string;
  2.   const MailData: TStrings; const Username, Password: string): Boolean;
  3. var
  4.   SMTP: TSMTPSend;
  5.   s, t, hos, por, ssl: string;
  6.   nps, nss: integer;
  7. begin
  8.   // modified by Patyi
  9.   //   SMTPHost parameter is expanded with TLS/SSL enable part, example :
  10.   //   for GMail host parameter: 'smtp.gmail.com:465&SSL'
  11.   // where ':' is port separator as before, '&' is the new for TLS/SSL enabling separator.
  12.  
  13.   Result := False;
  14.   SMTP := TSMTPSend.Create;
  15.   try
  16.     s := Trim(SMTPHost); // string represent host, port and TLS/SSL option
  17.     nps := Pos(':', s);  // ':' port separator position
  18.     nss := Pos('&', s);  // '&' TLS/SSL  separator position
  19.     if (nps = 0) and (nss = 0) then begin // only host is given, no port, no TLS/SSL
  20.       hos := s;
  21.       por := '';  // default port is 25
  22.       ssl := '';
  23.     end else if (nps > 0) and (nss = 0) then begin // host and port is given, no TLS/SSL
  24.       hos := Trim(Copy(s, 1, nps-1));
  25.       por := Trim(Copy(s, nps+1, 10));  // users port
  26.       ssl := '';
  27.     end else if (nps = 0) and (nss > 0) then begin // host and TLS/SSL is given, no port
  28.       hos := Trim(Copy(s, 1, nss-1));
  29.       por := '587'; // if TLS then default port is 587
  30.       ssl := Trim(Copy(s, nss+1, 10)); // TLS or SSL
  31.       if Pos('SSL', ssl) > 0 then
  32.         por := '465'; // if SSL then default port is 465
  33.     end else if (nps > 0) and (nss > 0) then begin // host, port and TLS/SSL given
  34.       hos := Trim(Copy(s, 1, nps-1));
  35.       por := Trim(Copy(s, nps+1, nss-nps-1)); // users port
  36.       ssl := Trim(Copy(s, nss+1, 10));  // TLS or SSL for enabling TLS/SSL
  37.     end;
  38.  
  39.     // if you need SOCKS5 support, uncomment next lines:
  40.  //   SMTP.Sock.SocksIP := '127.0.0.1';
  41.  //   SMTP.Sock.SocksPort := '1080';
  42.     if Pos('TLS', ssl) > 0 then  // if TLS is enabled
  43.       SMTP.AutoTLS := True;
  44.     if Pos('SSL', ssl) > 0 then  // if SSL is enabled
  45.       SMTP.FullSSL := True;
  46.  
  47.     SMTP.TargetHost := hos;
  48.     if por > '' then  // if different port is given
  49.       SMTP.TargetPort := por;
  50.     SMTP.Username := Username;
  51.     SMTP.Password := Password;
  52.  
  53.     if SMTP.Login then begin
  54.       if SMTP.MailFrom(GetEmailAddr(MailFrom),
  55.                        Length(MailData.Text)) then begin
  56.         s := MailTo;
  57.         repeat
  58.           t := GetEmailAddr(Trim(FetchEx(s, ',', '"')));
  59.           if t <> '' then
  60.             Result := SMTP.MailTo(t);
  61.           if not Result then
  62.             Break;
  63.         until s = '';
  64.         if Result then
  65.           Result := SMTP.MailData(MailData);
  66.       end;
  67.       SMTP.Logout;
  68.     end;
  69.   finally
  70.     SMTP.Free;
  71.   end;
  72. end;      
  73.  

The problem is that, the smtp.gmail.com authentication is fail, can't connect to server and mail is not delivered !
Is maybe is missing same SSL unit ? On Wiki is mentioned that ssl_openssl unit  but I cnt't find.

I would really appreciate it if someone could help me !
« Last Edit: February 26, 2018, 03:49:20 pm by patyi »

patyi

  • Full Member
  • ***
  • Posts: 168
Re: Synapse smtp.gmail.com GMail SSL authentication problem
« Reply #1 on: February 26, 2018, 01:16:22 pm »
Sorry I forgot to mention:
XUbuntu 17.10 i386, FpcUpDeluxe: Fpc i386 Fixes, Lazarus i386 Fixes. (updated yesterday)
The solution have to be multiplatform.

patyi

  • Full Member
  • ***
  • Posts: 168
Re: Synapse smtp.gmail.com GMail SSL authentication problem
« Reply #2 on: February 26, 2018, 03:48:39 pm »
Hi all !

I found a solution ! In DataModule have to add this units : SMTPSend, MIMEPart, MIMEMess, ssl_openssl   !!!
ssl_openssl.pas defines the SSL plugin type, and uses openssl shared librari from system, describe was here :
http://www.ararat.cz/synapse/doku.php/public:howto:sslplugin

The last thing what have do is defining the path to synapse installation where is ssl_openssl.pas and ssl_openssl_lib.pas was found, or simply copy those two files to your project folder, what was I do.

After this is working like a charm: smtp.gmail.com TLS port 587 !

Thanks, and hope this help for somebody to save time !
« Last Edit: February 26, 2018, 03:50:47 pm by patyi »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Synapse smtp.gmail.com GMail SSL authentication problem
« Reply #3 on: February 26, 2018, 04:15:25 pm »
Hi all !

I found a solution ! In DataModule have to add this units : SMTPSend, MIMEPart, MIMEMess, ssl_openssl   !!!
ssl_openssl.pas defines the SSL plugin type, and uses openssl shared librari from system, describe was here :
http://www.ararat.cz/synapse/doku.php/public:howto:sslplugin

The last thing what have do is defining the path to synapse installation where is ssl_openssl.pas and ssl_openssl_lib.pas was found, or simply copy those two files to your project folder, what was I do.

After this is working like a charm: smtp.gmail.com TLS port 587 !

Thanks, and hope this help for somebody to save time !
You could have simply read the documentation: the use of openssl is very well documented.
Specialize a type, not a var.

patyi

  • Full Member
  • ***
  • Posts: 168
Re: [SOLVED] Synapse smtp.gmail.com GMail SSL authentication problem
« Reply #4 on: February 26, 2018, 10:56:26 pm »
"If all your attempts failed, read the instruction manual"
Murphys low.  :D

 

TinyPortal © 2005-2018