android - 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 发送?
解决方案
你需要用FileAttachment
..attachments.add(FileAttachment(_image1))
推荐阅读
- python - 我无法导入 tensorflow-gpu
- oracle - Pl/SQL 拆分表
- javascript - JavaScript - 输出变量到控制台防止滚动
- vuejs2 - 为什么 Vue 的 v-if 不及时渲染块?
- python - Python - 如何在 bs4 中使用 select()
- python - “fuse”、“melt”、“regroup” pandas 数据帧几乎相同的行
- matlab - 具有线索引时如何将图像分为顶部和底部
- java - Java:`sun.awt.image` 包是否已弃用?
- bash - 如何使用 bash 从除第一个文件夹之外的所有路径中获取
- haskell - `symbolVal` 中的错误类型在运行时获取类型级字符串