首页 > 解决方案 > Flutter 邮件程序正在发送重复的电子邮件,同时发送两封

问题描述

我必须构建一个具有发送电子邮件功能的应用程序,我决定使用邮件程序。但是在测试之后,我收到了两封相同的电子邮件,我又向 cc 发送了一封电子邮件,该电子邮件也收到了两封电子邮件。有没有办法让我的应用程序只发送一封电子邮件而不是两封?

void _mailer() async {

String username = '**@gmail.com';

String password = '**';

final smtpServer = gmail(username, password);

final message = Message()

  ..from = Address(username, 'Your name')
  ..recipients.add('**@outlook.com')
  ..ccRecipients.add('**@yahoo.com')
  ..subject = 'Test Dart Mailer library ::  :: ${DateTime.now()}'
  ..text = 'This is the plain text.\nThis is line 2 of the text part.'
  ..attachments = attachments;

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

var connection = PersistentConnection(smtpServer);

await connection.send(message);

await connection.close();
}

标签: flutteremailmailer

解决方案


这个函数似乎发送了 1 封电子邮件:

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

这 3 行代码正在发送第二封电子邮件:

var connection = PersistentConnection(smtpServer);

await connection.send(message);

await connection.close();

所以我刚刚删除了最后 3 行代码,现在我的应用只发送一封电子邮件


推荐阅读