Recent

Author Topic: again synapse and gmail  (Read 3295 times)

krolikbest

  • Full Member
  • ***
  • Posts: 172
again synapse and gmail
« on: May 23, 2021, 01:32:36 pm »
Hi,

I thought that it's simply thing, use this code from link https://wiki.freepascal.org/Synapse#Sending_email to send e-mail to my google account but something i can't or don't understand. So my code looks like (simplified)
Code: Pascal  [Select][+][-]
  1. function TForm1.SendMail(MailFrom, MailTo , MailData: string): Boolean;
  2. var
  3.   SMTP: TSMTPSend;
  4.   sl:TStringList;
  5.  
  6. begin
  7.    Result:=False;
  8.   SMTP:=TSMTPSend.Create;
  9.   sl:=TStringList.Create;
  10.   try
  11.     sl.text:=Maildata;
  12.     SMTP.UserName:='myaccount@gmail.com';
  13.     SMTP.Password:='mypasswd';
  14.     SMTP.TargetHost:='smtp.gmail.com';
  15.     SMTP.TargetPort:='465';
  16.     SMTP.AutoTLS:=true;
  17.     SMTP.FullSSL:=true;
  18.     if SMTP.Login then
  19.     begin
  20.       result:=SMTP.MailFrom(MailFrom, Length(MailData)) and
  21.          SMTP.MailTo(MailTo) and
  22.          SMTP.MailData(sl);
  23.       SMTP.Logout;
  24.       showmessage('logged');
  25.     end;
  26.   finally
  27.     SMTP.Free;
  28.     sl.Free;
  29.   end;
  30. end;
and button.OnClick looks like
Code: Pascal  [Select][+][-]
  1. SendMail('otheraddr@wp.pl', 'myaccount@gmail.com','122334');

In uses there are units  smtpsend, ssl_openssl, openssl, opensslsockets;   
I wonder if username is the same as 'myaccount@gmail.com' in my case?. But  i get message 'logged' so I assume that it is. But nothing is received in thunderbird. Maybe someone has got similar problem with gmail. Other my account on different host works fine using similar code.

Regards,
   

lucamar

  • Hero Member
  • *****
  • Posts: 4138
Re: again synapse and gmail
« Reply #1 on: May 23, 2021, 02:28:22 pm »
If your source host (wp.pl) stores the "sent" messagges, have you checked whether the message is there? That would meant it was sent but for some reason Google rejected it; you might have received a message back at wp.pl telling you so and why, but it doesn't always happen.

That would be my first step: make sure the message was sent and wahtthe reply (if any) was.

Note that GMail (as most other big mailers) uses lots of "security" and "anti-spam" features (like rather stringent source host checks) that interfere a lot with "home-grown" mail programs, so your message might have got rejected (silently or not) or sent to your gmail spam folder.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

krolikbest

  • Full Member
  • ***
  • Posts: 172
Re: again synapse and gmail
« Reply #2 on: May 23, 2021, 03:44:40 pm »
The mailbox of wp.pl is empty in every folder. So it looks like the message has been sent?
There is workaround if at google account I choose less secure access but consider this way as bad solution.

nanobit

  • Full Member
  • ***
  • Posts: 112
Re: again synapse and gmail
« Reply #3 on: May 23, 2021, 07:29:29 pm »
For investigation you can collect feedback from TSMTPSend.fullResult.text after each command (Login, mailFrom(), ...).

krolikbest

  • Full Member
  • ***
  • Posts: 172
Re: again synapse and gmail
« Reply #4 on: May 23, 2021, 08:38:40 pm »
Exactly it's what I did and in progress (530 5.7.0). Sooner or later I'll resolve it so please take this subject as a closed. Im sure I have something wrong with my e-mail address.

rvk

  • Hero Member
  • *****
  • Posts: 4479
Re: again synapse and gmail
« Reply #5 on: May 24, 2021, 10:54:07 am »
There is workaround if at google account I choose less secure access but consider this way as bad solution.
You shouldn't use the less secure option. It is better to create a specific App-password. You can use that with your username. The App-password is then specifically for your app and sending mail.

It's also not the recommended way but much much better than setting the "less secure" option.
https://support.google.com/accounts/answer/185833?hl=en

krolikbest

  • Full Member
  • ***
  • Posts: 172
Re: again synapse and gmail
« Reply #6 on: May 25, 2021, 10:53:02 am »
My workaround - there  must be some sender address (let's say myaddr1@wp.pl) and receiver is gmail address (mygmailaddr@gmail.com). My code slightly differs from my previous (I added Mime)
Code: Pascal  [Select][+][-]
  1. function TForm1.SendMail(User, Password, MailFrom, MailTo, SMTPHost,
  2.   SMTPPort: string; MailData: string): Boolean;
  3. var
  4.   SMTP: TSMTPSend;
  5.   sl:TStringList;
  6.   MimeMsg : TMimeMess;
  7.  
  8. begin
  9.   Result:=False;
  10.   SMTP:=TSMTPSend.Create;
  11.   sl:=TStringList.Create;
  12.   MimeMsg := TMimeMess.Create;
  13.   sl.text:=Maildata;
  14.   try
  15.     MimeMsg.Header.From := MailFrom;
  16.     MimeMsg.Header.ToList.Add(mailTo);
  17.     MimeMsg.Header.Subject := 'Test message';
  18.     MimeMsg.AddPartText(sl,nil);
  19.  
  20.     SMTP.UserName:=User;
  21.     SMTP.Password:=Password;
  22.     SMTP.TargetHost:=SMTPHost;
  23.     SMTP.TargetPort:=SMTPPort;
  24.     mimeMsg.EncodeMessage;
  25.     SMTP.FullSSL:=true; //wp.pl use only SSL
  26.     if SMTP.Login then
  27.     begin
  28.       showmessage(smtp.ResultString);
  29.  
  30.       SMTP.MailFrom(MailFrom, Length(mimeMsg.Lines.Text));
  31.       SMTP.MailTo(MailTo);
  32.       if SMTP.MailData(mimeMsg.Lines) then
  33.         SMTP.Logout;
  34.       showmessage(smtp.ResultString);
  35.     end;
  36.   finally
  37.     freeAndNil(mimeMsg);
  38.     SMTP.Free;
  39.     sl.Free;
  40.   end;
  41. end;                          
  42.  

and in button.OnClick
Code: Pascal  [Select][+][-]
  1.  SendMail('myaddr1@wp.pl', 'passwd', 'myaddr1@wp.pl', 'mygmailaddr@gmail.com', 'smtp.wp.pl', '465','New Hope');      

This is raw code so one has to polish and to adapt to the needs. As you see is used additional address in order to send to gmail address. On the other hand this solution doesn't leave messages on wp.pl domain so in my case that is good. Of course one can it change using Mime.

Regards,


 

TinyPortal © 2005-2018