问题描述
我正在创建一个Flutter应用程序,该应用程序通过SMTP发送电子邮件。
使用仿真器,电子邮件可以正常工作。
当我构建APK并将其安装在手机上时,电子邮件不会发送。
我尝试了不同的端口,allowInsecure(true / false),ignoreBadCertificate(true / false)。 我什至尝试使用其他SMTP服务器(而不是Gmail服务器),结果也一样。
我正在使用以下内容: 邮件:^ 3.0.4
sendEmail(String emailFrom,String emailTo,String emailSubject,String emailBody) async {
String smtpServerName = 'smtp.gmail.com';
int smtpPort = 465;
String smtpUserName = 'mygmail@gmail.com';
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}');
}
}
}
更新:当手机连接到WiFi时,电子邮件会发送。 在我看来,现在好像运营商正在阻止电子邮件。 我的运营商是AT&T。注意:我已经尝试使用端口25,465和587。
另一个更新:我开始在数据库中记录错误。 连接到Wifi时,一切都很好。 断开Wifi连接时,记录的错误消息是“用户名/密码/凭据不正确”。
那么,为什么使用Wifi可以正常使用凭据,但是与Wifi断开连接时无效? 注意:无论有没有Wifi,我都能正常使用所有网络服务。
解决方法
最可能的原因是您没有在android\app\src\main\AndroidManifest.xml
的主AndroidManifest.xml文件中添加Internet权限。
<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>
电子邮件在您的模拟器上正常运行的原因是,通过在调试器和配置文件清单中添加了颤动添加了Internet权限,当应用程序在模拟器上运行时,默认情况下会使用该清单