颤抖的“邮件程序"API多个图像附件 [英] Flutter "Mailer" API Multiple Image Attachments
问题描述
我正在制作一个简单的应用程序,用于从用户那里获取个人信息和图像数量,只需单击一下按钮即可通过后端邮件API发送这些图像.到目前为止,我可以通过邮件获取和发送FormData,但是我无法弄清楚如何发送图像数组.
I'm making a simple app for getting personal information from the user and number of images to send them through backend mail API with a one click of a button. So far, I can get and send the FormData through mail but I couldn't figure it out the how to send an array of images.
我尝试了几种API,但"Mailer"似乎最适合SMTP.至于代码,我试图将"File"类转换为String或List,但是这些都不对我有用.我不是中级编码员,所以请对我好一点:)
I have tried several API's but "Mailer" seems to best for SMTP. As for the code, I tried to convert the "File" class to String or List but none of those have worked for me. I'am not a intermediate coder so be kind with me :)
这就是我使用"image_picker"获取图像的方式
That's how I get the images using "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();
}
这是我得到的错误,无论尝试如何,始终都是类型"错误.
This is the error I get and whatever I try it's always the "type" error.
不能将参数类型"File"分配给参数类型"Attachment".
那么,如何从用户那里获取多个图像文件并通过邮件API发送?
So, how can I get multiple image files from user and send through mail API?
推荐答案
您需要使用 FileAttachment
..attachments.add(FileAttachment(_image1))
这篇关于颤抖的“邮件程序"API多个图像附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!