问题
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)
The code below :
@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";
}
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.
回答1:
Something has messed up the Content-Type header, probably your application. How are you setting the Content-Type? There's lots of sample code on the JavaMail FAQ and available for download form the JavaMail project page in case you need help figuring out how to add an attachment.
回答2:
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.
Thank you very much.
来源:https://stackoverflow.com/questions/16714525/javax-mail-internet-parseexception-expected-got-files