java - 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 服务器抱怨垃圾邮件:)
解决方案
推荐阅读
- swift - 带有 collectionView 的 Hero ViewController 动画 - swift
- mobile - 从一个 PWA 重定向到另一个 PWA 会显示地址栏
- python - vue js中的多步表单类别指定字段
- javascript - jQuery 动画如何从头开始?
- azure - 是否可以使用托管标识从 Azure 应用服务访问 Azure 函数?
- prefect - 如何使用 prefect 创建审批工作流?
- html - 如何将 2 个 div 放在一起?
- python - 寄予厚望:如果未提供 root_directory,则 base_directory 必须是绝对路径
- kubernetes - Helm 未通过身份验证
- arm - IMX.6 Sabre lite 上 ARM FPU (Neon-vfvp3) 上的浮动精度问题