在 Flutter 中通过邮件发送文件附件

时间:2021-01-31 21:05:52

标签: flutter dart

所以我想在邮件库中的电子邮件中附加一个文件。正如我在邮件程序描述中看到的那样,它说您可以附加文件,但我不知道如何:

final message = Message()
..from = Address(username)
..recipients.add('dest@example.com') //recipent email
..ccRecipients.addAll(['destCc1@example.com', 'destCc2@example.com']) //cc Recipents emails
..bccRecipients.add(Address('bccAddress@example.com')) //bcc Recipents emails
..subject = 'Test Dart Mailer library :: ? :: ${DateTime.now()}' //subject of the email
..text = 'This is the plain text.\nThis is line 2 of the text part.' //body of the email

如何在此处附加文件(例如 csv 文件)?

1 个答案:

答案 0 :(得分:0)

您可以查看this

  Iterable<Attachment> toAt(Iterable<String> attachments) =>
      (attachments ?? []).map((a) => FileAttachment(File(a)));

  // Create our message.
  final message = Message()
    ..from = Address('$username@gmail.com', 'My name ?')
    ..recipients.addAll(toAd(tos))
    ..ccRecipients.addAll(toAd(args[ccArgs] as Iterable<String>))
    ..bccRecipients.addAll(toAd(args[bccArgs] as Iterable<String>))
    ..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>"
..attachments.addAll(toAt(args[attachArgs] as Iterable<String>));