Swift_TransportException错误求助:登录成功后发邮件遇smtp.gmail.com连接超时
Hey there! Let's work through this Gmail SMTP connection timeout issue together—this is a super common problem, and I’ve got a few tried-and-true fixes that should get you up and running.
The Connection timed out #110 error almost always means your server can’t reach Gmail’s SMTP server at all. Here’s how to confirm:
- SSH into your server and run this command:
ping smtp.gmail.com- If you get no response or packet loss, your server’s firewall is likely blocking outgoing traffic on ports 587 (Gmail’s recommended TLS port) or 465 (SSL port).
- Check with your hosting provider too—many shared hosts block these ports to prevent spam. You might need to ask them to unblock the ports, or switch to using their internal SMTP server instead.
It’s easy to mix up settings, so verify these are 100% correct:
- SMTP Host:
smtp.gmail.com - Port: Use 587 with TLS encryption, or 465 with SSL
- Authentication: Must be enabled (you’ll need your full Gmail address as the username)
- Encryption: Match the port—TLS for 587, SSL for 465
If you have two-factor authentication (2FA) turned on for your Gmail account, your regular password won’t work for SMTP. You need a dedicated App Password instead:
- Go to your Google Account > Security > App Passwords (this option only shows up if 2FA is active)
- Select "Mail" as the app, and your server/device as the device
- Generate the 16-character app password, then plug that into your SMTP password field instead of your regular password.
While this isn’t secure for permanent use, enabling "Less secure app access" can quickly tell you if this is the issue:
- Go to your Google Account > Security > Less secure app access
- Toggle it on, then try sending the email again. If it works, switch back to using an App Password immediately—never leave this enabled long-term.
Google often blocks sign-in attempts from unfamiliar servers. Head to your Gmail’s "Recent security activity" page (you’ll find this in your account settings under Security) to see if there’s a notification about a blocked sign-in. If so, click "Allow" for that attempt and mark your server’s IP as trusted.
If none of the above works, the Gmail API is a reliable alternative. It doesn’t rely on SMTP ports, so it’s great for environments where those ports are restricted. You’ll need to set up credentials in the Google Cloud Console, but there are plenty of code examples (depending on your framework) to get you started.
内容的提问来源于stack exchange,提问作者user6891871

