问题描述
我必须构建一个具有发送电子邮件功能的应用程序,所以我决定使用mailer。但是在测试之后,我收到了两封相同的电子邮件,然后又向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();
}
解决方法
此功能似乎发送了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}');
}
}
这三行代码正在发送第二封电子邮件:
var connection = PersistentConnection(smtpServer);
await connection.send(message);
await connection.close();
所以我刚刚删除了最后三行代码,现在我的应用仅发送一封电子邮件