Unable to connect to the SMTP server: context deadline exceeded

Hi, When I set SMTP in system control,

In [Test Connection], it always says:
unable to connect to the SMTP server: context deadline exceeded.

I’m using my Gmail account and I’m sure my account and password is right.
Could you tell me what I should do?
Thank you!

Hi meninblack,

this most likely is caused by firewall issues preventing you from connecting to the SMTP server. Most providers block outbound connections to TCP port 25 nowadays, so please try to use port 587 f.ex. for your gmail account and also verify that the connection is possible on the console, first.

To do so, you can use telnet (f.ex.) to try to establish a connection:

root@host:~# telnet smtp.gmail.com 25
Trying 142.250.149.109...
Connected to smtp.gmail.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP c7-20020a9d6847000000b006393ea22c1csm1915106oto.16 - gsmtp
QUIT
221 2.0.0 closing connection c7-20020a9d6847000000b006393ea22c1csm1915106oto.16 - gsmtp
Connection closed by foreign host.

When you can see the greeting string from gmail, you know that the connection can be established, type QUIT to exit the session.
If that doesn’t work, try to switch to port 587 (instead of 25 in the above example).
Does any of these two ports work for you?
What IP address/domain name did you use for the SMTP server and can you maybe share a screenshot of the settings if just switching the port does not fix your issue?

Hi, Aagriesser,
Thank you for telling me this.
I tried telnet gmail smtp,
and it works.

OK, the port is set to 465 in your settings and you only tried to connect on port 25. Can you also try to connect to port 465 via telnet or can you switch to port 25 or 587 (if this one works too) in your Mattermost settings?

Telnet

root@De:/www/wwwroot# telnet smtp.gmail.com 465
Trying 142.251.2.109...
Connected to smtp.gmail.com.
Escape character is '^]'.
Connection closed by foreign host.
root@De:/www/wwwroot# telnet smtp.gmail.com 587
Trying 142.251.2.109...
Connected to smtp.gmail.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP jb1-20020a170903258100b00176953f7997sm1506846plb.158 - gsmtp
^C^]

465 seems connected but no respose.
587 is ok

Mattermost:
I’ve tried 25,465,587 and get the same result.

Connection unsuccessful: Connection unsuccessful: unable to connect to the SMTP server: context deadline exceeded

OK, you will not get a prompt on 465, because it’s the connection port with implicit SSL/TLS, so you would have to talk encrypted to the other party, but yes, the connection itself looks good.

What is your deployment model? Did you deploy using containers and is the container probably not allowed to connect to the internet or is the container having name resolution problems?
Can you see anything in the mattermost.log file when you try to run the connection test?

No, I’m not using docker.I can see the log file.
The lastest log is

{"timestamp":"2022-09-04 07:19:24.110 Z","level":"error","msg":"Connection unsuccessful: unable to connect to the SMTP server: context deadline exceeded","caller":"web/context.go:117","path":"/dddddd/api/v4/email/test","request_id":"r3kz9sab97fa8dddddd","ip_addr":"127.0.0.1","user_id":"1hrdiffddddd","method":"POST","err_where":"testEmail","http_code":500,"err_details":""}

When you use port 465, please set Connection security to TLS. For ports 25 or 587 use STARTTLS. Does any of these combinations work?

Thank you and Thank god, both 465 and 587 works!
But it still have errors.

Connection unsuccessful: Connection unsuccessful: authentication failed: 535 5.7.8 Username and Password not accepted. 

Is it indicates the network is good?

I’m pretty sure my account and password is right…
I can use them access the web mail of gmail.
Do you know what may cause it?

The username needs to be your full gmail address, not just the part in front of the @, so f.ex. meninblack@gmail.com, if that is your address.

Got it!
Acturally, I found it’s the problem of mail service provider.
I tried Gmail,Yahoo mail, Yadex…
They have different strange response.
Finally Hotmail works…

Thank you so much Agriesser!

Hi,it seems still have problem when invite a friend,
I saw them in the mattermost.log.
Is it because receiver’s mailbox believe I’m a spamer?
Any suggestions?

{"timestamp":"2022-09-05 04:45:08.234 Z","level":"error","msg":"Failed to send invite email successfully ","caller":"email/email.go:473","error":"failed to close connection to the SMTP server: 554 5.2.0 STOREDRV.Submission.Exception:OutboundSpamException; Failed to process message due to a permanent exception with message [BeginDiagnosticData]WASCL UserAction verdict is not None. Actual verdict is RefuseQuota, ShowTierUpgrade. OutboundSpamException: WASCL UserAction verdict is not None. Actual verdict is RefuseQuota, ShowTierUpgrade.[EndDiagnosticData] [Hostname=SG2PR03MB3449.apcprd03.prod.outlook.com]"}
{"timestamp":"2022-09-05 04:45:50.030 Z","level":"info","msg":"SimpleWorker: Job is complete","caller":"jobs/base_workers.go:88","worker":"ExpiryNotify","job_id":"a464pxu6wtrn5cn99dtzkztw8r"}
{"timestamp":"2022-09-05 04:46:00.221 Z","level":"warn","msg":"Unrecognized config permissions tag value.","caller":"api4/config.go:431","tag_value":"sysconsole_write_*_read"}
{"timestamp":"2022-09-05 04:46:32.562 Z","level":"error","msg":"Failed to send invite email successfully ","caller":"email/email.go:473","error":"failed to close connection to the SMTP server: 554 5.2.0 STOREDRV.Submission.Exception:OutboundSpamException; Failed to process message due to a permanent exception with message [BeginDiagnosticData]WASCL UserAction verdict is not None. Actual verdict is RefuseQuota, ShowTierUpgrade. OutboundSpamException: WASCL UserAction verdict is not None. Actual verdict is RefuseQuota, ShowTierUpgrade.[EndDiagnosticData] [Hostname=SG2PR03MB3449.apcprd03.prod.outlook.com]"}

The configuration should have worked this way, not sure what the problem was with Gmail without seeing the error message.
And yes, the error message from Hotmail indicates that they think you’re a spammer - for whatever reason, hard to tell, to be honest.
What was the error message when you tried with Gmail and the username in the form of your full e-mail address? I did just try to set up one of my demo installations to send through my gmail account and this worked without issues (on port 587 with STARTTLS).

Problem Solved!
when I use gmail without 2-step, it always says:

I tried add 2-Step Verification and got an one-time App Password,
problem solved!

Ah, OK - this makes sense, didn’t think about MFA activated accounts in the first place. Glad to hear you’re up and running now.