提问人:Patel Milan 提问时间:4/13/2023 更新时间:4/13/2023 访问量:38
如何使用 gmail api 发送 https://www.googleapis.com/auth/gmail.send 使用邮件程序在 flutter 上发送电子邮件
How can send email on flutter using mailer using gmail api send https://www.googleapis.com/auth/gmail.send
问:
scope(['https://mail.google.com/']);
它可以正常工作,但谷歌建议使用scopes: ['email', 'https://www.googleapis.com/auth/gmail.send']);
要使用 Gmail API 并继续验证过程,您需要从 SMTP 协议中迁移出来。相反,应使用 https://www.googleapis.com/auth/gmail.send 范围。此范围仍为您的项目提供足够的访问权限以正常工作。
如果您的应用仅使用 SMTP 协议,请注意,仅将广泛访问 https://mail.google.com/ 范围用于使用 SMTP 协议发送电子邮件会违反最小范围策略。要使用 Gmail API 并继续执行验证过程,您需要迁移出 SMTP 协议,并改用敏感 https://www.googleapis.com/auth/gmail.send 范围。
pubspec.yaml
dependencies:
mailer: ^6.0.0
firebase_core: ^2.9.0
firebase_auth: ^4.4.0
google_sign_in: ^5.0.0
sendMail()
Future<void> sendEmail() async {
SmtpServer? smtpServer;
try {
// Setting up Google SignIn
final googleSignIn = GoogleSignIn.standard(
scopes: ['email', 'https://www.googleapis.com/auth/gmail.send']);
// Signing in
final account = await googleSignIn.signIn();
if (account == null) {
// User didn't authorize
return;
}
final auth = await account.authentication;
// Creating SMTP server from the access token
smtpServer = gmailSaslXoauth2(account.email,auth.accessToken!);
} on PlatformException catch (e) {
print(e);
}
final message = Message()
..from = Address('[email protected]', 'Your name')
..recipients.add('[email protected]')
..subject = 'Test Dart Mailer library'
..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>";
// Ready to send a message now
final sendReport = await send(message, smtpServer!);
}
未处理的异常:身份验证失败(代码:334)
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Authentication Failed (code: 334), response:
< eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
#0 _doAuthentication (package:mailer/src/smtp/smtp_client.dart:106:5)
<asynchronous suspension>
#1 connect (package:mailer/src/smtp/smtp_client.dart:136:5)
<asynchronous suspension>
#2 send (package:mailer/src/smtp/mail_sender.dart:93:20)
<asynchronous suspension>
#3 _MyHomePageState.sendEmail (package:send_mail/main.dart:102:24)
<asynchronous suspension>
答: 暂无答案
评论