首页 > 解决方案 > Flutter "Mailer" API 多图附件

问题描述

我正在制作一个简单的应用程序,用于从用户那里获取个人信息以及通过后端邮件 API 发送的图像数量,只需单击一下按钮即可。到目前为止,我可以通过邮件获取和发送 FormData,但我无法弄清楚如何发送图像数组。

我已经尝试了几个 API,但“Mailer”似乎最适合 SMTP。至于代码,我尝试将“文件”类转换为字符串或列表,但没有一个对我有用。我不是中级编码员,所以请善待我 :)

这就是我使用“image_picker”获取图像的方式

File _image1;

Future getImage1Camera() async {
    var image1 = await ImagePicker.pickImage(source: ImageSource.camera);

    setState(() {
     _image1 = image1; 
    });

  }

和“邮件”代码

void _mailer() async{

  if(!_formKey.currentState.validate()){
    return;
  }else{
    _formKey.currentState.save();
  }

  String gmailUsername = '**';
  String gmailPassword = '**';

  final smtpServer = gmail(gmailUsername, gmailPassword);

  final ceSendMail = Message()
  ..from = Address(gmailUsername, '')
  ..recipients.add('recipent')
  ..subject = 'Test'
  ..text = 'Plain Text'
  ..html = ''//Form Data
  ..attachments.add(_image1);//TODO: User input images

  try {
    final sendReport = await send(cekSendMail, 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}');
    }
  }
  // Create a smtp client that will persist the connection
  var connection = PersistentConnection(smtpServer);

  // Send the message
  await connection.send(cekSendMail);

  // close the connection
  await connection.close();

}

这是我得到的错误,无论我尝试什么,它总是“类型”错误。

The argument type 'File' can't be assigned to the parameter type 'Attachment'.

那么,如何从用户那里获取多个图像文件并通过邮件 API 发送?

标签: androidemailflutterdart

解决方案


你需要用FileAttachment

..attachments.add(FileAttachment(_image1))

推荐阅读