首页 > 解决方案 > java.mail 不附加文件,Android。错误:IOException

问题描述

我无法将文件附加到附件。

ERROR: IOException while sending message 

没有附件发送优秀我检查 File.exists() 是真的

我尝试了很多方法。没有一个是行不通的。同样的结果

...
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", mailhost);
props.put("mail.smtp.port", port);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.auth", "true");

Session session = Session.getInstance(props);
MimeMessage msg = new MimeMessage(session);
Multipart multipart = new MimeMultipart();
Transport transport = session.getTransport();

msg.setSender(new InternetAddress(sender));
msg.setSubject(subject);

MimeBodyPart part = new MimeBodyPart();
part.setText(body);


String file = new String("/sdcard/DCIM/2.jpg");
FileDataSource fileDataSource = new FileDataSource(file);
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(fileDataSource.getName());

if (new File(file).exists()) Log.e("sendMail", "file exists");

multipart.addBodyPart(part);
multipart.addBodyPart(attachmentPart);
msg.setContent(multipart);

msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));

transport.connect(mailhost, username, password);
transport.sendMessage(msg, msg.getAllRecipients());

解决了android权限问题和

attachmentPart.setHeader("Content-Type", "application/octet-stream");  

图像/jpeg smtp 服务器抱怨垃圾邮件:)

标签: javaandroidemailjakarta-mailreact-native-android

解决方案


推荐阅读