Recent

Author Topic: [Solved] LAMW, problem of sending email using the Synapse library  (Read 695 times)

Alexin

  • Newbie
  • Posts: 3
Hello everyone!

I am using IDE Laz4android 2.0.0 with LAMW.
It is necessary to send a simple text Email from my Android application.
To do this, I installed the Synapse libraries, using the SMTPSend, ssl_openssl modules (for encryption on port 465).
But this does not work for me under Android, everything compiles and starts without errors, but the emails do not go away.
 In the "libs \ armeabi-v7a" project folder, I put two ssl libraries “libcrypto.so" and "libssl.so” version 1.0.2j, tried other versions of these libraries, to no avail.
In the SendToRaw function of the SMTPsend.pas module, I checked that FullSSL is enabled:
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: string;
  6. begin
  7.   Result := False;
  8.   SMTP := TSMTPSend.Create;
  9.   try
  10. // if you need SOCKS5 support, uncomment next lines:
  11.     // SMTP.Sock.SocksIP := '127.0.0.1';
  12.     // SMTP.Sock.SocksPort := '1080';
  13. // if you need support for upgrade session to TSL/SSL, uncomment next lines:
  14.      //SMTP.AutoTLS := True;
  15. // if you need support for TSL/SSL tunnel, uncomment next lines:
  16.      SMTP.FullSSL := True;
  17.     SMTP.TargetHost := Trim(SeparateLeft(SMTPHost, ':'));
  18.     s := Trim(SeparateRight(SMTPHost, ':'));
  19.     if (s <> '') and (s <> SMTPHost) then
  20.       SMTP.TargetPort := s;
  21.     SMTP.Username := Username;
  22.     SMTP.Password := Password;
  23.     if SMTP.Login then
  24.     begin
  25.       if SMTP.MailFrom(GetEmailAddr(MailFrom), Length(MailData.Text)) then
  26.       begin
  27.         s := MailTo;
  28.         repeat
  29.           t := GetEmailAddr(Trim(FetchEx(s, ',', '"')));
  30.           if t <> '' then
  31.             Result := SMTP.MailTo(t);
  32.           if not Result then
  33.             Break;
  34.         until s = '';
  35.         if Result then
  36.           Result := SMTP.MailData(MailData);
  37.       end;
  38.       SMTP.Logout;
  39.     end;
  40.   finally
  41.     SMTP.Free;
  42.   end;
  43. end;
  44.  

The following is an excerpt from my code:

Code: Pascal  [Select][+][-]
  1.  
  2. Procedure SendMail (Host, Subject, pTo, From , TextBody, HTMLBody, login,password : string);
  3. var Msg : TMimeMess; // message
  4.     StringList : TStringList; // message content
  5.     MIMEPart : TMimePart; // parts of the message
  6. begin
  7.   Msg := TMimeMess.Create; // create new message
  8.   StringList := TStringList.Create;
  9.   try
  10.     // add headers
  11.     Msg.Header.Subject := Subject; // Message subject
  12.     Msg.Header.From := From; // name and address of sender
  13.     Msg.Header.ToList.Add(pTo); // recipient name and address
  14.     // create the root element
  15.     MIMEPart := Msg.AddPartMultipart('alternative', nil);
  16.     if length(TextBody)=0 then
  17.     // if format HTML
  18.       begin
  19.         StringList.Text := HTMLBody;
  20.         Msg.AddPartHTML(StringList, MIMEPart);
  21.       end
  22.     else
  23.     // if text format
  24.       begin
  25.         StringList.Text := TextBody;
  26.         Msg.AddPartText(StringList, MIMEPart);
  27.       end;
  28.     // encode and send
  29.     Msg.EncodeMessage;
  30.     // send
  31.     if smtpsend.SendToRaw(From,pTo,Host,Msg.Lines,login,password) then
  32.         ConnectForm.ShowMessage('Email Sent')
  33.     else
  34.         ConnectForm.ShowMessage('Email NOT Sent');
  35.  finally
  36.    Msg.Free;
  37.    StringList.Free;
  38.  end;
  39. end;
  40.                                        
  41. procedure TConnectForm.jButton1Click(Sender: TObject);
  42. begin
  43.  
  44.  SendMail('smtp.gmail.com:465',
  45.          'test email subject',
  46.          'recipient@gmail.com',
  47.          'sender@gmail.com’,
  48.         'The content of the letter in text format',
  49.         '',
  50.         'sender@gmail.com, 'password')
  51. end;          
  52.  
  53.  
  54.  

Under Windows I checked, everything works fine (using the openssl, libeay32.dll and ssleay32.dll libraries). How to make it work under Android? Maybe someone faced such a problem? 
In the folder with demos lazandroidmodulewizard, there were no examples of sending e-mail, there is only the jcMail component that can only receive e-mails, and not send. If anyone knows the solution, or shows a working example, I will be very grateful.
« Last Edit: May 06, 2020, 06:45:31 am by Alexin »

yuriy_sydorov

  • Full Member
  • ***
  • Posts: 156
Re: LAMW, problem of sending email using the Synapse library
« Reply #1 on: April 21, 2020, 01:31:22 pm »
Without exact error messages/codes and position where the error occurs it is not possible to help you.
Also it is needed to specify the FPC version and the Android version.

Alexin

  • Newbie
  • Posts: 3
Re: LAMW, problem of sending email using the Synapse library
« Reply #2 on: May 02, 2020, 09:01:31 pm »
Without exact error messages/codes and position where the error occurs it is not possible to help you.
Also it is needed to specify the FPC version and the Android version.
Hello.

Apparently, this happens when it tries to log in.
SMTP.Login (From the function "SendToRaw" I cited above). This step failed.

I tried to catch the error through LogCat ADB, but found nothing about this smtp connection.
Although Synapse safely compiles for arm-android, I guess that it cannot access the web.
Though, there is permission in AndroidManifest.xml:
   <uses-permission android: name = "android.permission.INTERNET" />


FPC version 3.0.4
Target SDK Version: Android 9 (API Level 28)
JDK: 1.8.0_241
Gradle 6.3
NDK: r10e
LAMW: 0.8.5
OS: Windows 10 (x64)
« Last Edit: May 02, 2020, 09:03:27 pm by Alexin »

Seenkao

  • Jr. Member
  • **
  • Posts: 69
Re: LAMW, problem of sending email using the Synapse library
« Reply #3 on: May 02, 2020, 10:58:21 pm »
Use version FPC > 3.0.4

yuriy_sydorov

  • Full Member
  • ***
  • Posts: 156
Re: LAMW, problem of sending email using the Synapse library
« Reply #4 on: May 03, 2020, 11:33:22 am »
You need to use FPC 3.2 or FPC trunk.
The netdb unit is unusable in FPC 3.0.2 for android. That's why synapse can't resolve host names and unable to connect.
See known issues here:
https://wiki.freepascal.org/Android#Known_issues

Alexin

  • Newbie
  • Posts: 3
Re: LAMW, problem of sending email using the Synapse library
« Reply #5 on: May 05, 2020, 08:46:08 pm »
You need to use FPC 3.2 or FPC trunk.
The netdb unit is unusable in FPC 3.0.2 for android. That's why synapse can't resolve host names and unable to connect.
See known issues here:
https://wiki.freepascal.org/Android#Known_issues

I rebuild using FPC trunk and I got this error while compiling :
custompage.inc:139:34: error:
Operator is not overloaded: "Class Of TCustomTabControl" and "Boolean"


Then I tried installing version FPC 3.2.0, and the program was compiled without errors, now sending emails works.

Thank you so much, problem is solved. :)

 

TinyPortal © 2005-2018