Reputation: 1
I'm creating a Flutter app that sends an email via SMTP.
The email works fine using the emulator.
When I build an APK and install it on my phone, the email does not send.
I've tried different ports, allowInsecure (true/false), ignoreBadCertificate (true/false). I've even tried using a different SMTP server (not a gmail server), same results.
I'm using the following: mailer: ^3.0.4
sendEmail(String emailFrom, String emailTo, String emailSubject, String emailBody) async {
String smtpServerName = 'smtp.gmail.com';
int smtpPort = 465;
String smtpUserName = '[email protected]';
String smtpPassword = '**********';
final smtpServer = SmtpServer(
smtpServerName,
port: smtpPort,
ssl: true,
ignoreBadCertificate: false,
allowInsecure: false,
username: smtpUserName,
password: smtpPassword,
);
final message = Message()
..from = Address(emailFrom, emailFrom)
..recipients.add(emailTo)
..subject = emailSubject
..html = emailBody;
try {
final sendReport = await send(message, smtpServer);
print('Message sent: ' + sendReport.toString());
} on MailerException catch (e) {
print('Message not sent.');
for (var p in e.problems) {
print('Problem: ${p.code}: ${p.msg}');
}
}
}
Update: When the phone is connected to WiFi, the email does send. Now it seems to me like the carrier is blocking the email. My carrier is AT&T. Note: I've tried ports 25,465 and 587.
Another Update: I started logging the errors in the database. When connected to Wifi, everything is fine. When disconnecting Wifi, the error message logged is "Incorrect username / password / credentials".
So why are the credentials fine with Wifi, but not valid when disconnected from Wifi? Note: All the web service calls I'm making work fine with and without Wifi.
Upvotes: 0
Views: 715
Reputation: 1670
This is most likely due to you not adding the Internet permission in your main AndroidManifest.xml file at android\app\src\main\AndroidManifest.xml
.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.new_app">
<!-- Add the following line
-->
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
The reason the email works fine on your emulator is because the internet permission is added by flutter in the debug and profile manifests which used by default when the app is running on your emulator
Upvotes: 0