从flutter应用程序发送电子邮件而不实际登录gmail帐户



我想在订单确认时向用户发送一封电子邮件。为此,我使用mailer 3.2.1软件包。https://pub.dev/packages/mailer

在下面代码片段的下面一行,我得到提示说'gmail' is deprecated and shouldn't be used. Favor gmailUserXoauth2 as username/password is considered a Less-Secure-Apps.

final smtpServer = gmail(username, password);

在上面的行中用gmailUserXoauth2替换gmail后,我得到了The method 'gmailUserXoauth2' isn't defined

以下是的代码片段

String username = 'xxx@gmail.com';
String password = 'xxx';
final smtpServer = gmail(username, password);
// Create our message.
final message = Message()
..from = Address(username, 'Your name')
..recipients.add('destination@example.com')
..ccRecipients.addAll(
['destCc1@example.com', 'destCc2@example.com'])
..bccRecipients.add(Address('bccAddress@example.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}');
}
}

在谷歌上搜索这个问题时,我发现了这个解决方案。https://stackoverflow.com/a/58732280/7290043该解决方案给出了以下的建议

  1. 启用";访问不太安全的应用程序";在没有OAuth 2.0的情况下使用Gmail
  2. 使用google_sign_in软件包登录谷歌帐户,然后发送电子邮件

但我不想做这两件事。我不想在使用google_sign_in包登录谷歌后发送电子邮件

我现在该怎么办?

您可以按照这里的指南操作。Flutter应用程序发送的电子邮件需要用户登录才能进行身份验证。XOAUTH2在指南中提供的示例代码中使用,它将用户重定向到登录页面进行身份验证。

相关内容

最新更新