Java:发送附件电子邮件时出现异常

标签 java email exception smtp jakarta-mail

我在用 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);
    }
}

有不对的地方请指出。 谢谢

最佳答案

首先我使用 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();

关于Java:发送附件电子邮件时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40934382/

相关文章:

php - `[Exception] DateTime::__construct(): ` 依赖系统的时区设置是不安全的

java - Java 中类似于 C/C++ INT_MAX 宏的是什么

java - 如何生成具有交替颜色的 Jlist

email - 使用 EC2 或网络邮件/电子邮件服务提供商的 AWS Route 53 电子邮件设置

Linux 发送邮件 - 命令行

java - 如何在tomcat的catalina.out中使用Spring MVC获取错误消息

java - 编译.java文件时javac.exe做什么?

java - 如果 start() 出现异常,则终止 Jetty 服务器线程池

MacOSX : new mail with attachment

sql - 我无法在 PrestaShop v1.6.1.0 中添加产品