提问人:Amedeo Braggio 提问时间:4/1/2023 最后编辑:Amedeo Braggio 更新时间:4/1/2023 访问量:59
在android中使用java发送邮件时出错(带附件)
Error sending mail with java in android (with attachment )
问:
我正在尝试通过 Android 中的 Java(在 AsyncTask 中)发送带有附件的邮件,我有这个方法:
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "psw");
}
});
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress("[email protected]@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
message.setSubject("Subject line");
message.setText("Email content");
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("test");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileName);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(source.getName());
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(attachmentBodyPart);
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new RuntimeException(ex);
}
在文件名上,我保存了文件名
FileOutputStream outputStream = context.openFileOutput(nameExelFile, Context.MODE_PRIVATE);
我得到的错误是:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.gattolfo.gestionaluber, PID: 6179
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$4.done(AsyncTask.java:415)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
Caused by: java.lang.RuntimeException: javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;
boundary="----=_Part_0_82827468.1680314088984"
at com.gattolfo.gestionaluber.DataManager.DbRappresentation.Registri.MailSender.sendMailInstant(MailSender.java:296)
at com.gattolfo.gestionaluber.DataManager.DbRappresentation.Registri.MailSender.doInBackground(MailSender.java:141)
at com.gattolfo.gestionaluber.DataManager.DbRappresentation.Registri.MailSender.doInBackground(MailSender.java:48)
at android.os.AsyncTask$3.call(AsyncTask.java:394)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
... 4 more
Caused by: javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;
boundary="----=_Part_0_82827468.1680314088984"
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1350)
at javax.mail.Transport.send0(Transport.java:231)
at javax.mail.Transport.send(Transport.java:100)
at com.gattolfo.gestionaluber.DataManager.DbRappresentation.Registri.MailSender.sendMailInstant(MailSender.java:292)
... 8 more
Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;
boundary="----=_Part_0_82827468.1680314088984"
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:878)
at javax.activation.DataHandler.writeTo(DataHandler.java:300)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1670)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1889)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1300)
... 11 more
我正在使用
implementation 'com.sun.mail:android-mail:1.6.7'
implementation 'com.sun.mail:android-activation:1.6.7'
我还尝试过:
implementation 'com.sun.mail:jakarta.mail:2.0.1'
请帮忙!!
我的 PC 上的相同代码 (Windows Java) 工作正常,我认为问题出在 android 内存的输入/输出上
[编辑]
我注意到添加一个附件是这样的:
MimeBodyPart messageBodyPart = new MimeBodyPart();
String jsonContent = "{\"name\": \"John Doe\", \"email\": \"[email protected]\"}";
messageBodyPart.setContent(jsonContent, "application/json");
从完全相同的错误中,它不会给出错误的唯一方法是如果我不添加这样的附件:
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailReciver));
message.setSubject("Report utilizzo cassette");
message.setText("message");
Transport.send(message);
答: 暂无答案
评论
DataSource source = new FileDataSource(fileName)
不能只使用文件名。您必须提供完整的路径。