我正在尝试在用户提交表单时将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连接(package:mailer / src / smtp / smtp_client.dart:135:11) E /颤振(6901): E / flutter(6901):#2发送(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发送(package:mailer / src / smtp / mail_sender.dart:90:24) E / flutter(6901):#5邮件(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发送(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连接(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(软件包: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 /颤振(6901):
我确定用户名和密码正确。
答案 0 :(得分:0)
您必须启用"Access for less secure apps"才能在不使用OAuth 2.0的情况下使用gmail,否则它将把客户端标记为不安全。可以在“帐户权限”块中的帐户设置的“安全性”选项卡上找到此选项。