我无法将文件附加到附件。
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");
image / jpeg smtp服务器抱怨垃圾邮件:)