【问题标题】:Java: Getting Exception while sending attachment emailJava:发送附件电子邮件时出现异常
【发布时间】:2017-04-17 11:49:16
【问题描述】:

我在用 java 发送电子邮件时遇到运行时异常 UnsupportedDataTypeException。这是详细的例外情况

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"

我该如何处理这个异常?

我正在使用这段代码:完整代码

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);
    }
}

请指出我错的地方。 谢谢

【问题讨论】:

  • 你真的需要覆盖你的变量 messageBodyPart

标签: java email exception smtp jakarta-mail


【解决方案1】:

首先我使用Javamail API 1.4.6 但现在使用Javamail API Version 1.5.0 或更高版本,上面的相同代码可以正常工作。所以,现在有了 API 1.5.0,我可以成功发送附件了。

编辑:当我使用 API 1.4.6 时

Transport.send(message);

它不起作用但是我们也可以使用 API 1.5.0 或更高版本

Transport.send(message);

而不是

SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
            t.connect("smtp.gmail.com", senderMail, pass);
            t.sendMessage(message, message.getAllRecipients());
            t.close();

【讨论】:

猜你喜欢
  • 2018-10-17
  • 2017-05-03
  • 2019-06-05
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多