javax.mail.internet.ParseException:预期的"/",有文件 [英] javax.mail.internet.ParseException: Expected "/", got files
问题描述
尝试发送带有文件附件的电子邮件时出现错误:
I am getting an error while trying to send an email with file attachment :
javax.mail.internet.ParseException: Expected '/', got files
at javax.mail.internet.ContentType.<init>(ContentType.java:102)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1331)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)
at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1354)
at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2107)
at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2075)
at javax.mail.Transport.send(Transport.java:123)
下面的代码:
@Override
public void execute(String filePath) {
try {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session mailSession = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(GMAIL_ACCOUNT, GMAIL_PASSWORD);
}
});
//We create a new mail message
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress("esombugma@gmail.com"));
//We constitute the list of recipients
List<InternetAddress> recipList = new ArrayList<>();
for(String address : this.getRecipients()) {
recipList.add(new InternetAddress(address));
}
InternetAddress[] iaTab = new InternetAddress[recipList.size()];
iaTab = recipList.toArray(iaTab);
msg.setRecipients(Message.RecipientType.TO, iaTab);
msg.setSubject(this.getSubject());
msg.setSentDate(new Date());
//Create multi-part
Multipart multipart = new MimeMultipart();
//Create a message part
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(this.getMessage(), this.getSubject());
multipart.addBodyPart(msgBodyPart);
//Adds attachment
if((new File(filePath)).exists()) {
MimeBodyPart attachment = new MimeBodyPart();
try {
attachment.attachFile(new File(filePath));
} catch (IOException ex) {
Logger.getLogger(EmailSender.class.getName()).log(Level.SEVERE, null, ex);
}
attachment.setFileName((new File(filePath)).getName());
multipart.addBodyPart(attachment);
}
msg.setContent(multipart);
Transport.send(msg);
} catch (MessagingException ex) {
Logger.getLogger(EmailSender.class.getName()).log(Level.SEVERE, null, ex);
}
}
生成文件的代码在这里:
And the code that generate the file is here :
public String getMissingReportName() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhh");
return "C://Report//Report_" + sdf.format(new Date()) + ".xls";
}
然后,示例报告文件应为:Report_20130512
.
Then a sample report file should be : Report_20130512
for example.
有人可以向我解释此错误的根源是什么. 谢谢.
Can someone explain to me what could be the orrigine of this error. Thank you.
推荐答案
我发现了错误.它来自我自己的代码,恰好来自此行msgBodyPart.setContent(this.getMessage(), this.getSubject());
.
参数this.getSubject()
的女巫应该是我的电子邮件的主题,它是由NetBeans IDE自动完成的,但我没有看到它(因为一周,所以是OMG).
我终于给msgBodyPart.setText(this.getMessage())
打了电话,它工作正常.
I found, the error. It come from my own code, exactly from this line msgBodyPart.setContent(this.getMessage(), this.getSubject());
.
The paramater this.getSubject()
witch is supposed to be the subject of my email, was autocompleted by my NetBeans IDE and I didn't saw it (since a week... OMG).
I have finally called msgBodyPart.setText(this.getMessage())
and it works fine.
非常感谢您.
这篇关于javax.mail.internet.ParseException:预期的"/",有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!