如何在Flutter中发送带附件的邮件?

Nic*_*ick 6 dart flutter

我发现名为 mailer3 的 dart 插件:“^1.1.9”。之前我在移动临时目录中创建了一个图像。在 Flutter 移动应用程序中,我尝试使用 mailer3 插件将这张保存的图片作为邮件发送。邮件到达目的地,我没有收到错误,但似乎在处理过程中丢失了附件。

\n\n

在 Dart 中它工作得很好并且也发送附件。在 flutter 中,我可以使用临时目录在应用程序中显示图像,但无法附加到邮件。

\n\n

图像位置位于设备的临时文件夹中:

\n\n
    \n
  • \'/data/user/0/com.myApp.myApp/app_flutter/20180700087.jpg\'
  • \n
\n\n

我可以使用下面的代码显示图像:

\n\n
    \n
  • 新的 FileImage(文件(\'$newDekontImage\'),
  • \n
\n\n

错误:

\n\n
E/flutter (21184): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:\nE/flutter (21184): FileSystemException: Cannot open file, path = \'/data/user/0/com.myApp.myApp/app_flutter/20180700087.jpg\' (OS Error: No such file or directory, errno = 2)\n
Run Code Online (Sandbox Code Playgroud)\n\n

如何在 Flutter 中发送带有附件的邮件并提供此问题中提供的信息?

\n\n

颤振代码:

\n\n
// TODO: SEND MAIL\nvoid _sendMail() async {\n  if (!_formKey.currentState.validate()) {\n    return;\n  } else {\n    _formKey.currentState.save();\n\n    var _options = new GmailSmtpOptions()\n    ..username = \xe2\x80\x9cmymailaddress@gmail.com"\n    ..password = \xe2\x80\x9cmyPassword\xe2\x80\x9d;\n\n    var _emailTransport = new SmtpTransport(_options);\n    var _envelope = new Envelope()\n    ..from = "mymailaddress@gmail.com"\n    ..recipients.add(_receiverMailAddress)\n      ..subject = "${_userDekontDetails[0][0].toString()} - Receipt\xe2\x80\x9d\n      ..attachments.add(await new Attachment(file: await new File(\'$newDekontImage\')))\n      ..text = "${_userDekontDetails[0][0].toString()} - Receipt"\n      ..html = \'<h3>${_userDekontDetails[0][0].toString()} Receipt.</h3>\'\n          \'<p>Hi, registered under my name, I am sending the receipt (${widget._currentUserReceiptNo}) with attached to this mail.</p>\'\n          \'<p></p>\'\n          \'<h5>Regards, </br></h5>\'\n          \'${_userDekontDetails[0][0].toString()}\';\n\n    _emailTransport.send(_envelope)\n      ..then((envelope) => print(\'Email sent\'))\n      ..catchError((e) => print(\'Error occured: $e\'));\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Oma*_*att 1

截至撰写本文时,mailer3 插件已过时,mailer是用于发送电子邮件的最新插件。邮件程序插件当前包含 mailer2 和 mailer3 的重要修复。我建议选择使用 mailer 包而不是 mailer3。

这是从 mailer3 到 mailer 的代码片段的端口

_sendMail(String username, String accessToken) async {
  // Read https://pub.dev/documentation/mailer/latest/smtp_server_gmail/gmailSaslXoauth2.html
  var _emailTransport = gmailSaslXoauth2(username, accessToken);

  var _envelope = new Message()
    ..from = "mymailaddress@gmail.com"
    ..recipients.add("recepient@gmail.com")
    ..subject = '{EMAIL_SUBJECT_GOES_HERE}'
    // Read https://pub.dev/documentation/mailer/latest/mailer/FileAttachment-class.html
    ..attachments
        .add(FileAttachment(File('{FILE_PATH}')))
    ..text = '{PLAIN_TEXT_GOES_HERE}'
    ..html = '{HTML_CONTENT_GOES_HERE}';

  send(_envelope, _emailTransport)
    ..then((envelope) => print('Email sent'))
    ..catchError((e) => print('Error occured: $e'));
}
Run Code Online (Sandbox Code Playgroud)