此代码在运行时返回"Message not sent“,并且不将消息发送到电子邮件。
我想用sendMail功能自动发送邮件到gmail。
我从dard pub和github复制了这段代码。
sendMail() async {
String username = '*******@gmail.com';
String password = '*********';
final smtpServer = gmail(username, password);
final message = Message()
..from = Address(username)
..recipients.add('********@gmail.com')
..subject = 'Test Dart Mailer library :: ? :: ${DateTime.now()}'
..text = 'This is the plain text.\nThis is line 2 of the text part.'
..html = "<h1>Test</h1>\n<p>Hey! Here's some HTML content</p>";
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}');
}
}
}
发布于 2020-12-09 09:09:05
邮件不会被发送,因为您使用的是普通的Gmail密码。只需生成应用程序密码即可。应用程序密码是一个16位数的密码,它允许非Google应用程序或设备访问您的Google帐户。
对于signing in using app passwords,请执行以下步骤。希望这篇文章能有所帮助!
https://stackoverflow.com/questions/65204382
复制相似问题