Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Attempt to send email using AWS results in Socket closed by peer

79 views
Skip to first unread message

Juan Rayas

unread,
Sep 1, 2021, 2:58:51 PM9/1/21
to
Hi,

I am teaching myself Ada and tried to create a simple program that sends an email if today's date equals my birthday :).

I am using Gnat Studio 2021, Community edition, Ubuntu 20.0.4. I downloaded, built and installed AWS. I am sending the email as follows:

procedure SendMail is
Status : AWS.SMTP.Status;
Auth : aliased constant SMTP.Authentication.Plain.Credential :=
SMTP.Authentication.Plain.Initialize
("myAccount", "myPassword");
Isp : SMTP.Receiver;

begin
New_Line;
Isp :=
SMTP.Client.Initialize
("smtp.gmail.com", Port => 465,
Credential => Auth'Unchecked_Access);

SMTP.Client.Send
(Isp, From => SMTP.E_Mail ("Me", "myAccount"),
To => SMTP.E_Mail ("Me", "myAccount"),
Subject => "AWS test",
Message => "This is a test",
Status => Status);

if not SMTP.Is_Ok (Status) then
Text_IO.Put_Line
("Can't send message :" & SMTP.Status_Message (Status));
end if;
end SendMail;

myAccount and myPassword are removed for the purposes of this post.

When I run my program, the SendEmail procedure fails with the following message:

raised AWS.SMTP.SERVER_ERROR : raised AWS.NET.SOCKET_ERROR : Receive : Socket closed by peer

Does anyone know how to fix this when using the gmail server? Or perhaps I am using AWS incorrectly?

Thanks,
Juan

Dmitry A. Kazakov

unread,
Sep 1, 2021, 3:43:10 PM9/1/21
to
On 2021-09-01 20:58, Juan Rayas wrote:

> Does anyone know how to fix this when using the gmail server?

No, but I guess that you must configure the SMTP client for TLS or else
opportunistic TLS (StartTLS). Most mail servers do not accept
unencrypted connections. I do not use AWS and its SMTP, maybe somebody
who does might help you with it.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

Richard Iswara

unread,
Sep 1, 2021, 11:29:41 PM9/1/21
to
Use a public email test server for testing program. Places like these:
https://www.bing.com/search?form=MOZLBR&pc=MOZI&q=email+test+servers. It
will show what's the received and sent responses.
The Gmail server marked your message as spam or worse and simply ignored
your message.

Doctor Who

unread,
Sep 2, 2021, 12:03:13 AM9/2/21
to
port 465 wants a TLS connection.

Randy Brukardt

unread,
Sep 2, 2021, 6:20:02 PM9/2/21
to
"Dmitry A. Kazakov" <mai...@dmitry-kazakov.de> wrote in message
news:sgol48$r20$1...@gioia.aioe.org...
...
> Most mail servers do not accept unencrypted connections.

That's news to me, as my mail server (you know, the one that runs
Ada-Comment and the other ARG lists) doesn't support encrypted connections.
I don't have any problem sending to GMail (I send copies of most of my
personal mail there). Something else must be wrong.

Randy.


Randy Brukardt

unread,
Sep 2, 2021, 6:22:56 PM9/2/21
to

"Doctor Who" <d...@tardis.org> wrote in message
news:57j0jgd920r1al3eh...@4ax.com...
...
> port 465 wants a TLS connection.

Good point. One uses port 25 for ordinary e-mail. One does not need to
authenticate to most public e-mail servers (of course, if you don't, you'll
be subjected to increased spam checks, but that usually isn't a problem if
you aren't sending spam).

Randy.


Dmitry A. Kazakov

unread,
Sep 3, 2021, 3:32:29 AM9/3/21
to
The question is with what settings. There are hundreds of combinations
of Port x unencrypted|StartTLS|TLS x authentication method.

The port 465 was intended for TLS, then it was depreciated. Then they
introduced other ports. Legacy mail servers pissed that all on etc.

From experience, getting a SMTP client working is real pain.

Doctor Who

unread,
Sep 3, 2021, 4:59:33 AM9/3/21
to
he is better off using his own provider's mail server on port 25, it
will accept username/password as authentication.

Juan Rayas

unread,
Sep 3, 2021, 2:11:43 PM9/3/21
to
Hi all,

thank you for your comments and responses.

I tried changing the port number to 25, using the default port number in the SMTP.Client.Initialize procedure, and using other port numbers such as 587. I even tried just initializing the SMT server and sending the email (without authentication) as indicated in https://docs.adacore.com/aws-docs/aws/working_with_mails.html.

In all cases, I get the following error:

Can't send message :530 5.7.0 Must issue a STARTTLS command first. u7sm5705829pju.13 - gsmtp

The problem is I don't know how to send the STARTTLS command with AWS. Anyone know how?

My past experience was mostly with real-time embedded systems, so I don't have a lot of experience with email servers. I didn't expect this to be so difficult. Since this was mostly a toy to learn Ada, I may move on to other aspects of Ada for now.

Thanks for all your comments.
--Juan

Dmitry A. Kazakov

unread,
Sep 3, 2021, 2:31:07 PM9/3/21
to
On 2021-09-03 20:11, Juan Rayas wrote:

> In all cases, I get the following error:
>
> Can't send message :530 5.7.0 Must issue a STARTTLS command first. u7sm5705829pju.13 - gsmtp
>
> The problem is I don't know how to send the STARTTLS command with AWS. Anyone know how?

There should be some settings parameter instructing the client to send
StartTLS right after it connects to the server. At least in my SMTP
implementation it is so.

But again, either port 587 or StartTLS, both require TLS, e.g. over
GNUTLS or else OpenSSL.

You need to tell AWS that you are going to use one them, specify the
certificates, the keys etc. You must really read the AWS documentation
regarding secure connections.

Doctor Who

unread,
Sep 3, 2021, 3:33:19 PM9/3/21
to
you have to change mail server:

Isp :=
SMTP.Client.Initialize
("smtp.gmail.com", Port => 465,
Credential => Auth'Unchecked_Access);

don't use gmail, use your provider mail server ...

Juan Rayas

unread,
Sep 3, 2021, 4:29:48 PM9/3/21
to
Hi all,

I changed the SMTP server to my ISP server (instead of Gmail), and changed the port back to 25. This worked!!

Thanks for your help and suggestions. Now on to learning other aspects of Ada.

--Juan

Simon Wright

unread,
Sep 3, 2021, 4:35:14 PM9/3/21
to
Juan Rayas <juan.m...@gmail.com> writes:

> Isp :=
> SMTP.Client.Initialize
> ("smtp.gmail.com", Port => 465,
> Credential => Auth'Unchecked_Access);

I succeed sending mail via gmail.com using port 587.

No useful further details on encryption - this was for the membership
d/b for a small club, and it's in Python/sqlite.

self.server = smtplib.SMTP(host=self.server_details['host'],
port=self.server_details['port'])
self.server.ehlo()
self.server.starttls()
self.server.ehlo()
self.server.login\
(user=server_details['user'],
password=keyring.get_password\
('u3a-email', server_details['user']))

Dmitry A. Kazakov

unread,
Sep 3, 2021, 5:13:40 PM9/3/21
to
On 2021-09-03 22:35, Simon Wright wrote:
> Juan Rayas <juan.m...@gmail.com> writes:
>
>> Isp :=
>> SMTP.Client.Initialize
>> ("smtp.gmail.com", Port => 465,
>> Credential => Auth'Unchecked_Access);
>
> I succeed sending mail via gmail.com using port 587.
>
> No useful further details on encryption - this was for the membership
> d/b for a small club, and it's in Python/sqlite.

GNUTLS and OpenSSL have ways to tell to use the system certificates.
E.g. if your Python application uses GNUTLS it would call

gnutls_certificate_set_x509_system_trust

and that should be enough to make successful handshaking afterwards.

> self.server = smtplib.SMTP(host=self.server_details['host'],
> port=self.server_details['port'])
> self.server.ehlo()
> self.server.starttls()

Here it switches transport to GNUTLS or OpenSSL and these perform TLS
handshaking. The following commands run over encrypted channel.

But with the port 587 you should actually begin with TLS straight away.
StartTLS is for servers which allow both secure and insecure
connections. This is usually the port 465. If you are OK with no
encryption you greet with HELO and then proceed with login. If you want
a secure connection you greet with EHLO and then send StartTLS.

> self.server.ehlo()
> self.server.login\
> (user=server_details['user'],
> password=keyring.get_password\
> ('u3a-email', server_details['user']))

This is very low-level. AWS design is far better, IMO.
0 new messages