I'm getting runtime exception UnsupportedDataTypeException
while sending an email in java. Here is the exception in detail
Exception in thread "main" 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_764977973.1480687764115"
How can I tackle this exception?
I'm using this piece of code: complete code
public static void main(String[] args) {
String senderMail = "inzi769@gmail.com";
String recepMail = "inzi.programmer@gmail.com";
String pass = "*********";
String host = "smtp.gmail.com";
String filePath = "C:\\Users\\Inzimam\\Desktop\\helicopter_final.png";
sendJavaMail(senderMail, pass, recepMail, host, filePath);
}
private static void sendJavaMail(String senderMail, String pass, String recepMail, String host, String filePath) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");
// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(senderMail, pass);
}
});
session.setDebug(true);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(senderMail));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recepMail));
message.setSubject("Subject here");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("This is message body");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filePath);
messageBodyPart.setDataHandler(new DataHandler(source));
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
t.connect("smtp.gmail.com", senderMail, pass);
t.sendMessage(message, message.getAllRecipients());
t.close();
// Transport.send(message);
JOptionPane.showMessageDialog(null, "Message has been sent successfully!.");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
Please point out that where I'm wrong. Thanks
First I was using Javamail API 1.4.6 But Now with the Javamail API Version 1.5.0 or higher the same code above is working correctly. So, now with API 1.5.0 I'm able to send attachment successfully.
Edit: with API 1.4.6 when I used
Transport.send(message);
it did not work But with API 1.5.0 or higher we can also use
Transport.send(message);
instead of
SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
t.connect("smtp.gmail.com", senderMail, pass);
t.sendMessage(message, message.getAllRecipients());
t.close();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.