[SOLVED] Synapse smtp.gmail.com GMail SSL authentication problem lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- patyi · 2018-02-26 11:57 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses ..., SMTPSend, MIMEPart, MIMEMess; ... if FDataMod.SendEMail('sender@gmail.com', 'reciver@eunet.rs',                            'Subject', 'Body text TStrings', 'Attachments TStrings',                              'Username', 'password',                            'smtp.gmail.com', '465', 'SSL') then          ShowMessage('Sending OK !')        else          ShowMessage('Sending failed  !');  ...  Source of my function : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TFDataMod.SendEMail(const snd, rcv, sub: string;                           const msg, att: TStrings;                             const use, pas, hos, por, tsl: string): boolean;var  M : TMimeMess;  P : TMimePart;  i : integer;  po, ss : string;begin  M := TMimeMess.Create;  try    M.Header.From := snd;    M.Header.ToList.CommaText := rcv;    M.header.Subject := sub;    P := M.AddPartMultipart('mixed', nil);    M.AddPartText(msg, P);    for i := 0 to att.Count-1 do      M.AddPartBinaryFromFile(att.Strings , P);    M.EncodeMessage;     po := '';    if por > '' then      po := ':'+por;     ss := '';    if (Pos('TLS', tsl) > 0) or (Pos('SSL', tsl) > 0) then      ss := '&'+tsl;     Result := SendToRaw(snd, rcv, hos+po+ss, M.Lines, use, pas);  finally    M.Free;  end;end; The modified Synapde smtpsend.pas unit, SendToRaw function Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function SendToRaw(const MailFrom, MailTo, SMTPHost: string;  const MailData: TStrings; const Username, Password: string): Boolean;var  SMTP: TSMTPSend;  s, t, hos, por, ssl: string;  nps, nss: integer;begin  // modified by Patyi  //   SMTPHost parameter is expanded with TLS/SSL enable part, example :  //   for GMail host parameter: 'smtp.gmail.com:465&SSL'  // where ':' is port separator as before, '&' is the new for TLS/SSL enabling separator.   Result := False;  SMTP := TSMTPSend.Create;  try    s := Trim(SMTPHost); // string represent host, port and TLS/SSL option    nps := Pos(':', s);  // ':' port separator position    nss := Pos('&', s);  // '&' TLS/SSL  separator position    if (nps = 0) and (nss = 0) then begin // only host is given, no port, no TLS/SSL      hos := s;      por := '';  // default port is 25      ssl := '';    end else if (nps > 0) and (nss = 0) then begin // host and port is given, no TLS/SSL      hos := Trim(Copy(s, 1, nps-1));      por := Trim(Copy(s, nps+1, 10));  // users port      ssl := '';    end else if (nps = 0) and (nss > 0) then begin // host and TLS/SSL is given, no port      hos := Trim(Copy(s, 1, nss-1));      por := '587'; // if TLS then default port is 587    ssl := Trim(Copy(s, nss+1, 10)); // TLS or SSL      if Pos('SSL', ssl) > 0 then        por := '465'; // if SSL then default port is 465    end else if (nps > 0) and (nss > 0) then begin // host, port and TLS/SSL given      hos := Trim(Copy(s, 1, nps-1));      por := Trim(Copy(s, nps+1, nss-nps-1)); // users port      ssl := Trim(Copy(s, nss+1, 10));  // TLS or SSL for enabling TLS/SSL    end;     // if you need SOCKS5 support, uncomment next lines: //   SMTP.Sock.SocksIP := '127.0.0.1'; //   SMTP.Sock.SocksPort := '1080';    if Pos('TLS', ssl) > 0 then  // if TLS is enabled      SMTP.AutoTLS := True;    if Pos('SSL', ssl) > 0 then  // if SSL is enabled      SMTP.FullSSL := True;     SMTP.TargetHost := hos;    if por > '' then  // if different port is given      SMTP.TargetPort := por;    SMTP.Username := Username;    SMTP.Password := Password;     if SMTP.Login then begin      if SMTP.MailFrom(GetEmailAddr(MailFrom),                     Length(MailData.Text)) then begin        s := MailTo;        repeat          t := GetEmailAddr(Trim(FetchEx(s, ',', '"')));          if t <> '' then            Result := SMTP.MailTo(t);          if not Result then            Break;        until s = '';      if Result then          Result := SMTP.MailData(MailData);      end;      SMTP.Logout;    end;  finally    SMTP.Free;  end;end;     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 ! ---------------------------------------------------------------------- patyi · 2018-02-26 12:16 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 · 2018-02-26 14:48 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 ! ---------------------------------------------------------------------- Thaddy · 2018-02-26 15:15 Quote from: patyi on February 26, 2018, 03:48:39 pmHi 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. ---------------------------------------------------------------------- patyi · 2018-02-26 21:56 "If all your attempts failed, read the instruction manual" Murphys low.