由于这些错误,Flutter Mailer 无法正常工作
Flutter Mailer isn't working due to these errors
我正在尝试在用户提交表单时自动将表单从 flutter app 发送到我的电子邮件。这是代码:
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:url_launcher/url_launcher.dart';
import 'package:mailer/mailer.dart';
import 'package:mailer/smtp_server.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
.
.
.
.
.
.
void mailer () async {
String username = 'some@gmail.com';
String password = '****************************';
final smtpServer = gmail(username, password);
// Use the SmtpServer class to configure an SMTP server:
// final smtpServer = SmtpServer('smtp.domain.com');
// See the named arguments of SmtpServer for further configuration
// options.
// Create our message.
final message = Message()
..from = Address(username, 'Ahmad Shaker')
..recipients.add('some@hotmail.com')
..ccRecipients.addAll(['destCc1@example.com', 'destCc2@example.com'])
..bccRecipients.add(Address('bccAddress@example.com'))
..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>";
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}');
}
}
// DONE
// Let's send another message using a slightly different syntax:
//
// Addresses without a name part can be set directly.
// For instance `..recipients.add('destination@example.com')`
// If you want to display a name part you have to create an
// Address object: `new Address('destination@example.com', 'Display name part')`
// Creating and adding an Address object without a name part
// `new Address('destination@example.com')` is equivalent to
// adding the mail address as `String`.
final equivalentMessage = Message()
..from = Address(username, 'Ahmad Eshbialat')
..recipients.add(Address('ahmad0shaker@gmail.com'))
..ccRecipients.addAll([Address('destCc1@example.com'), 'destCc2@example.com'])
..bccRecipients.add('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>";
final sendReport2 = await send(equivalentMessage, smtpServer);
// Sending multiple messages with the same connection
//
// Create a smtp client that will persist the connection
var connection = PersistentConnection(smtpServer);
// Send the first message
await connection.send(message);
// send the equivalent message
await connection.send(equivalentMessage);
// close the connection
await connection.close();
}
它给我这个错误:
I/flutter ( 6901): Message not sent.
E/flutter ( 6901): [ERROR:flutter/lib/ui/ui_dart_state.cc(144)] Unhandled Exception: Incorrect username / password / credentials
E/flutter ( 6901): #0 _doAuthentication (package:mailer/src/smtp/smtp_client.dart:105:5)
E/flutter ( 6901):
E/flutter ( 6901): #1 connect (package:mailer/src/smtp/smtp_client.dart:135:11)
E/flutter ( 6901):
E/flutter ( 6901): #2 send (package:mailer/src/smtp/mail_sender.dart:93:26)
E/flutter ( 6901): #3 _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:43:6)
E/flutter ( 6901): #4 send (package:mailer/src/smtp/mail_sender.dart:90:24)
E/flutter ( 6901): #5 mailer (package:Clowns_in_Amman/main.dart:991:29)
E/flutter ( 6901): #6 _asyncErrorWrapperHelper. (dart:async-patch/async_patch.dart:78:45)
E/flutter ( 6901): #7 _rootRunBinary (dart:async/zone.dart:1144:38)
E/flutter ( 6901): #8 _CustomZone.runBinary (dart:async/zone.dart:1037:19)
E/flutter ( 6901): #9 _FutureListener.handleError (dart:async/future_impl.dart:151:20)
E/flutter ( 6901): #10 Future._propagateToListeners.handleError
(dart:async/future_impl.dart:690:47)
E/flutter ( 6901): #11 Future._propagateToListeners (dart:async/future_impl.dart:711:24)
E/flutter ( 6901): #12 Future._completeError (dart:async/future_impl.dart:530:5)
E/flutter ( 6901): #13 _AsyncAwaitCompleter.completeError (dart:async-patch/async_patch.dart:36:15)
E/flutter ( 6901): #14 send (package:mailer/src/smtp/mail_sender.dart)
E/flutter ( 6901): #15 _asyncErrorWrapperHelper. (dart:async-patch/async_patch.dart:78:45)
E/flutter ( 6901): #16 _rootRunBinary (dart:async/zone.dart:1144:38)
E/flutter ( 6901): #17 _CustomZone.runBinary (dart:async/zone.dart:1037:19)
E/flutter ( 6901): #18 _FutureListener.handleError (dart:async/future_impl.dart:151:20)
E/flutter ( 6901): #19 Future._propagateToListeners.handleError
(dart:async/future_impl.dart:690:47)
E/flutter ( 6901): #20 Future._propagateToListeners (dart:async/future_impl.dart:711:24)
E/flutter ( 6901): #21 Future._completeError (dart:async/future_impl.dart:530:5)
E/flutter ( 6901): #22 _AsyncAwaitCompleter.completeError (dart:async-patch/async_patch.dart:36:15)
E/flutter ( 6901): #23 connect (package:mailer/src/smtp/smtp_client.dart)
E/flutter ( 6901): #24 _asyncThenWrapperHelper. (dart:async-patch/async_patch.dart:71:64)
E/flutter ( 6901): #25 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter ( 6901): #26 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 6901): #27 _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
E/flutter ( 6901): #28 Future._propagateToListeners.handleValueCallback
(dart:async/future_impl.dart:678:45)
E/flutter ( 6901): #29 Future._propagateToListeners (dart:async/future_impl.dart:707:32)
E/flutter ( 6901): #30 Future._completeWithValue (dart:async/future_impl.dart:522:5)
E/flutter ( 6901): #31 _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
E/flutter ( 6901): #32 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
E/flutter ( 6901): #33 Connection.close (package:mailer/src/smtp/connection.dart)
E/flutter ( 6901): #34 _asyncThenWrapperHelper. (dart:async-patch/async_patch.dart:71:64)
E/flutter ( 6901): #35 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter ( 6901): #36 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 6901): #37 _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
E/flutter ( 6901): #38 Future._propagateToListeners.handleValueCallback
(dart:async/future_impl.dart:678:45)
E/flutter ( 6901): #39 Future._propagateToListeners (dart:async/future_impl.dart:707:32)
E/flutter ( 6901): #40 Future._addListener. (dart:async/future_impl.dart:387:9)
E/flutter ( 6901): #41 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 6901): #42 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter ( 6901):
我确定用户名和密码是正确的。
您必须启用 "Access for less secure apps" 才能在没有 OAuth 2.0 的情况下使用 Gmail,否则,它会将客户端标记为不安全。此选项位于“帐户权限”部分帐户设置的“安全”选项卡上。
更新:您实际上无法为具有双向身份验证的帐户禁用此安全措施。
最好的方法是使用 google_sign_in package. In order to do this, you need to register the app (read package's docs and this page). Choose Gmail scopes from the table here 使用 Google 帐户登录(要发送消息,需要 https://www.googleapis.com/auth/gmail.send
)。登录成功后,您可以获取授权令牌并发送消息。
这是一个简单的例子。我还没有测试它(我有点懒得注册我的应用程序)但它应该可以工作。
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 = gmailXoauth2(auth.accessToken);
} on PlatformException catch (e) {
// TODO: Handle auth error
print(e);
}
// Ready to send a message now
final sendReport = await send(message, smtpServer);
我正在尝试在用户提交表单时自动将表单从 flutter app 发送到我的电子邮件。这是代码:
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:url_launcher/url_launcher.dart';
import 'package:mailer/mailer.dart';
import 'package:mailer/smtp_server.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
.
.
.
.
.
.
void mailer () async {
String username = 'some@gmail.com';
String password = '****************************';
final smtpServer = gmail(username, password);
// Use the SmtpServer class to configure an SMTP server:
// final smtpServer = SmtpServer('smtp.domain.com');
// See the named arguments of SmtpServer for further configuration
// options.
// Create our message.
final message = Message()
..from = Address(username, 'Ahmad Shaker')
..recipients.add('some@hotmail.com')
..ccRecipients.addAll(['destCc1@example.com', 'destCc2@example.com'])
..bccRecipients.add(Address('bccAddress@example.com'))
..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>";
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}');
}
}
// DONE
// Let's send another message using a slightly different syntax:
//
// Addresses without a name part can be set directly.
// For instance `..recipients.add('destination@example.com')`
// If you want to display a name part you have to create an
// Address object: `new Address('destination@example.com', 'Display name part')`
// Creating and adding an Address object without a name part
// `new Address('destination@example.com')` is equivalent to
// adding the mail address as `String`.
final equivalentMessage = Message()
..from = Address(username, 'Ahmad Eshbialat')
..recipients.add(Address('ahmad0shaker@gmail.com'))
..ccRecipients.addAll([Address('destCc1@example.com'), 'destCc2@example.com'])
..bccRecipients.add('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>";
final sendReport2 = await send(equivalentMessage, smtpServer);
// Sending multiple messages with the same connection
//
// Create a smtp client that will persist the connection
var connection = PersistentConnection(smtpServer);
// Send the first message
await connection.send(message);
// send the equivalent message
await connection.send(equivalentMessage);
// close the connection
await connection.close();
}
它给我这个错误:
I/flutter ( 6901): Message not sent. E/flutter ( 6901): [ERROR:flutter/lib/ui/ui_dart_state.cc(144)] Unhandled Exception: Incorrect username / password / credentials E/flutter ( 6901): #0 _doAuthentication (package:mailer/src/smtp/smtp_client.dart:105:5) E/flutter ( 6901): E/flutter ( 6901): #1 connect (package:mailer/src/smtp/smtp_client.dart:135:11) E/flutter ( 6901): E/flutter ( 6901): #2 send (package:mailer/src/smtp/mail_sender.dart:93:26) E/flutter ( 6901): #3 _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:43:6) E/flutter ( 6901): #4 send (package:mailer/src/smtp/mail_sender.dart:90:24) E/flutter ( 6901): #5 mailer (package:Clowns_in_Amman/main.dart:991:29) E/flutter ( 6901): #6 _asyncErrorWrapperHelper. (dart:async-patch/async_patch.dart:78:45) E/flutter ( 6901): #7 _rootRunBinary (dart:async/zone.dart:1144:38) E/flutter ( 6901): #8 _CustomZone.runBinary (dart:async/zone.dart:1037:19) E/flutter ( 6901): #9 _FutureListener.handleError (dart:async/future_impl.dart:151:20) E/flutter ( 6901): #10 Future._propagateToListeners.handleError (dart:async/future_impl.dart:690:47) E/flutter ( 6901): #11 Future._propagateToListeners (dart:async/future_impl.dart:711:24) E/flutter ( 6901): #12 Future._completeError (dart:async/future_impl.dart:530:5) E/flutter ( 6901): #13 _AsyncAwaitCompleter.completeError (dart:async-patch/async_patch.dart:36:15) E/flutter ( 6901): #14 send (package:mailer/src/smtp/mail_sender.dart) E/flutter ( 6901): #15 _asyncErrorWrapperHelper. (dart:async-patch/async_patch.dart:78:45) E/flutter ( 6901): #16 _rootRunBinary (dart:async/zone.dart:1144:38) E/flutter ( 6901): #17 _CustomZone.runBinary (dart:async/zone.dart:1037:19) E/flutter ( 6901): #18 _FutureListener.handleError (dart:async/future_impl.dart:151:20) E/flutter ( 6901): #19 Future._propagateToListeners.handleError (dart:async/future_impl.dart:690:47) E/flutter ( 6901): #20 Future._propagateToListeners (dart:async/future_impl.dart:711:24) E/flutter ( 6901): #21 Future._completeError (dart:async/future_impl.dart:530:5) E/flutter ( 6901): #22 _AsyncAwaitCompleter.completeError (dart:async-patch/async_patch.dart:36:15) E/flutter ( 6901): #23 connect (package:mailer/src/smtp/smtp_client.dart) E/flutter ( 6901): #24 _asyncThenWrapperHelper. (dart:async-patch/async_patch.dart:71:64)
E/flutter ( 6901): #25 _rootRunUnary (dart:async/zone.dart:1132:38) E/flutter ( 6901): #26 _CustomZone.runUnary (dart:async/zone.dart:1029:19) E/flutter ( 6901): #27 _FutureListener.handleValue (dart:async/future_impl.dart:137:18) E/flutter ( 6901): #28 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
E/flutter ( 6901): #29 Future._propagateToListeners (dart:async/future_impl.dart:707:32) E/flutter ( 6901): #30 Future._completeWithValue (dart:async/future_impl.dart:522:5) E/flutter ( 6901): #31 _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15) E/flutter ( 6901): #32 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13) E/flutter ( 6901): #33 Connection.close (package:mailer/src/smtp/connection.dart) E/flutter ( 6901): #34 _asyncThenWrapperHelper. (dart:async-patch/async_patch.dart:71:64)
E/flutter ( 6901): #35 _rootRunUnary (dart:async/zone.dart:1132:38) E/flutter ( 6901): #36 _CustomZone.runUnary (dart:async/zone.dart:1029:19) E/flutter ( 6901): #37 _FutureListener.handleValue (dart:async/future_impl.dart:137:18) E/flutter ( 6901): #38 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45) E/flutter ( 6901): #39 Future._propagateToListeners (dart:async/future_impl.dart:707:32) E/flutter ( 6901): #40 Future._addListener. (dart:async/future_impl.dart:387:9) E/flutter ( 6901): #41 _microtaskLoop (dart:async/schedule_microtask.dart:41:21) E/flutter ( 6901): #42 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5) E/flutter ( 6901):
我确定用户名和密码是正确的。
您必须启用 "Access for less secure apps" 才能在没有 OAuth 2.0 的情况下使用 Gmail,否则,它会将客户端标记为不安全。此选项位于“帐户权限”部分帐户设置的“安全”选项卡上。
更新:您实际上无法为具有双向身份验证的帐户禁用此安全措施。
最好的方法是使用 google_sign_in package. In order to do this, you need to register the app (read package's docs and this page). Choose Gmail scopes from the table here 使用 Google 帐户登录(要发送消息,需要 https://www.googleapis.com/auth/gmail.send
)。登录成功后,您可以获取授权令牌并发送消息。
这是一个简单的例子。我还没有测试它(我有点懒得注册我的应用程序)但它应该可以工作。
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 = gmailXoauth2(auth.accessToken);
} on PlatformException catch (e) {
// TODO: Handle auth error
print(e);
}
// Ready to send a message now
final sendReport = await send(message, smtpServer);