Flutter Mailer isn't working due to these errors

丶灬走出姿态 提交于 2021-01-29 13:30:28

问题


I am trying to send a form from flutter app to my email automatically when the user submit the form. here's the code :

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();


}

it gives me this error :

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):

I am sure the user name and password are correct.


回答1:


You have to enable "Access for less secure apps" to use Gmail without OAuth 2.0, otherwise, it will flag the client as insecure. This option can be found on the Security tab of account settings in the "Account permissions" block.

UPD: You can't actually disable this security measure for accounts with 2-way authentication.

The best way is to sign in with Google account using the 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 (to send a message, https://www.googleapis.com/auth/gmail.send is required). After a successful sign in, you can take the auth token and send a message.

Here's a brief example. I haven't tested it (I'm a bit lazy to register my app) but it should be working.

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);


来源:https://stackoverflow.com/questions/58731933/flutter-mailer-isnt-working-due-to-these-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!