【问题标题】:Flutter Mailer isn't working due to these errors由于这些错误,Flutter Mailer 无法正常工作
【发布时间】:2020-03-03 01:01:06
【问题描述】:

当用户提交表单时,我正在尝试将表单从 Flutter 应用程序自动发送到我的电子邮件。这是代码:

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):消息未发送。 E/flutter(6901):[错误:flutter/lib/ui/ui_dart_state.cc(144)]未处理的异常:用户名/密码/凭据不正确 E/flutter (6901): #0 _doAuthentication (package:mailer/src/smtp/smtp_client.dart:105:5) E/颤振(6901): E/flutter (6901): #1 connect (package:mailer/src/smtp/smtp_client.dart:135:11) E/颤振(6901): E/flutter(6901):#2 发送(包: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 发送(包:mailer/src/smtp/mail_sender.dart:90:24) E/flutter(6901):#5 邮件(包裹:Clowns_in_Amman/main.dart:991:29) E/颤振(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/颤振(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 发送(包:mailer/src/smtp/mail_sender.dart) E/颤振(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/颤振(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 连接(包:mailer/src/smtp/smtp_client.dart) E/颤振(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/颤振(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(包:mailer/src/smtp/connection.dart) E/颤振(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/颤振(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/颤振(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/颤动​​(6901):

我确定用户名和密码正确。

【问题讨论】:

    标签: flutter dart flutter-dependencies mailer


    【解决方案1】:

    您必须启用"Access for less secure apps" 才能在没有 OAuth 2.0 的情况下使用 Gmail,否则,它会将客户端标记为不安全。此选项可以在“帐户权限”块中帐户设置的“安全”选项卡上找到。

    UPD:您实际上无法为具有双向身份验证的帐户禁用此安全措施。

    最好的方法是使用 google_sign_in 包使用 Google 帐户登录。为此,您需要注册应用程序(阅读包的文档和this page)。从here 表中选择 Gmail 范围(要发送邮件,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);
    

    【讨论】:

    • @AhmadShaker 没问题。如果对您有帮助,请点赞/接受答案。
    • 有什么方法可以让这个连接成为安全连接吗?
    • @JoseTapizquent 你必须使用 OAuth。 Mailer 库有需要 OAuth 令牌的 gmailXoauth2 工厂,你应该看看。
    • @IgorKharakhordin 能不能给个例子或者图书馆的链接谢谢
    • @Merym 我已经更新了答案。请看一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 2020-02-25
    • 2018-06-13
    • 1970-01-01
    相关资源
    最近更新 更多