I’m working on an Android app and need to implement email functionality using Gmail’s SMTP server. I have the JavaMail library set up with the required JAR files (mail.jar, activation.jar, and additional.jar). Here’s my current implementation:
I keep getting this error: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25. The connection times out when trying to reach port 25. What could be causing this issue and how can I fix it?
Your Transport connection logic is the problem. You’re setting port 587 in properties but then manually connecting through Transport, which defaults to port 25. Since you’ve already set up authentication in your emailSession, this is way simpler.
Ditch the entire manual Transport connection block and just use:
Transport.send(message);
This automatically uses your session properties. Also, your SSL config is conflicting. For port 587 with STARTTLS:
Port 587 uses STARTTLS, not direct SSL. Want SSL? Use port 465 instead.
Critical: run this in AsyncTask or a background thread. NetworkOnMainThreadException will crash your app on newer Android versions. And make sure you’ve got INTERNET permission in AndroidManifest.xml.
Check your AndroidManifest.xml for network security config - newer Android versions block cleartext by default, which screws with SMTP even on secure ports. That timeout on port 25 tells me your session properties aren’t getting picked up by the transport layer. Try explicitly setting mail.smtp.socketFactory.class to javax.net.ssl.SSLSocketFactory and mail.smtp.socketFactory.port to 587. Fixed the same connection drops for me.
You’re mixing two different session objects. You create emailSession with proper auth, then immediately override it with getDefaultInstance() - which ignores your authenticator and properties. Ditch the second session creation. Just use emailSession throughout. And you don’t need to manually connect to Transport with authenticated sessions - call Transport.send(message) directly. Also, make sure you’re using an App Password, not your regular Gmail password. If you’ve got 2FA enabled, regular passwords won’t work with SMTP anymore thanks to Google’s security changes. I hit similar timeout issues last year - turned out my hosting provider was blocking outbound SMTP ports. If you’re still getting timeouts after fixing the session thing, test from a different network to rule out firewall blocks.
Your code sets port 587 in properties but JavaMail’s still trying to connect to port 25. That’s because getDefaultInstance() ignores your custom properties.
Honestly, JavaMail + Gmail auth in Android is a nightmare. App passwords, OAuth tokens, library compatibility, network permissions - it’s endless.
I switched to Latenode for this. Set up a webhook that takes email data from your Android app, then Latenode handles all the SMTP mess server-side. No more JavaMail headaches, no hardcoded credentials, works across Android versions.
Your app just makes an HTTP request with email details. Way cleaner.
Been wrestling with Gmail SMTP on Android for years too. Your problem’s in the Properties config - you’ve got conflicting SSL settings that mess up the transport layer. For port 587, Gmail wants STARTTLS, not direct SSL. Ditch those ssl.enable and ssl.required properties. Replace them with mail.smtp.starttls.enable set to true. Also, you’re not handling Gmail’s newer auth right. Regular passwords don’t work reliably since Google’s security updates. If you’ve got 2FA on, generate an App Password through your Google Account settings. That port 25 error? Your transport connection isn’t reading session properties correctly. When you manually connect, pass the properties explicitly. Or just use Transport.send(message) - it’ll respect your session config. One more thing - test on a real device, not the emulator. Emulators have weird network restrictions that don’t match real-world behavior.
You’re creating two sessions but using the wrong one. Your emailSession has the right config, but then you create currentSession with getDefaultInstance() which ignores your properties.
Just use emailSession for your MimeMessage:
message = new MimeMessage(emailSession);
And skip the manual Transport connection:
Transport.send(message);
That said, embedding SMTP credentials in your Android app is asking for trouble. Anyone can decompile your APK and steal those credentials. Plus you’re dealing with app passwords, network configs, SSL certificates, and Android compatibility issues.
I’ve built tons of mobile apps that need email. Much cleaner to move email logic server side. I use Latenode workflows that handle SMTP operations through secure webhooks.
Your Android app just sends an HTTP request with email details. Latenode receives it and handles the Gmail SMTP connection with proper credentials. No JavaMail dependencies, no hardcoded passwords, works across all Android versions.
Way more maintainable and secure than fighting SMTP configs in mobile code.