通过yahoo邮件发送multiPart邮件导致javax.activation.UnsupportedDataTypeException:没有MIME类型multipart / mixed的对象DCH;

时间:2014-09-24 08:58:05

标签: java javamail

我在论坛上寻求帮助的时间已经很久了,但我已经在这个问题上苦苦挣扎了几天。

阅读所有论坛所做的所有ppl但没有改变。

这是我的代码

我做错了什么?

顺便说一句,我使用的是javamail 5.1和java 8,并且已经发送了文本消息,所以没有属性问题

// Get system properties
    Properties properties = System.getProperties();
    // Setup mail server
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", mailHost);
    properties.put("mail.smtp.user", mailUserName);
    properties.put("mail.smtp.password", mailPassword);
    properties.put("mail.smtp.port", "587");
    properties.put("mail.smtp.auth", "true");

    // Get the default Session object.
    Session session = Session.getDefaultInstance(properties);

    try{
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(mD.mailUserName));

        // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

        Multipart multiPart = new MimeMultipart();
        MimeBodyPart textPart = new MimeBodyPart();
        textPart.setText("This is actual message", "text/html; charset=utf-8");

        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(html, "text/html; charset=utf-8");
        multiPart.addBodyPart(textPart);
        multiPart.addBodyPart(htmlPart);
        message.setContent(multiPart);

        // Send message
        Transport transport = session.getTransport("smtp");
        transport.connect( mD.mailHost,mD.mailUserName,mD.mailPassword);

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader( javax.mail.Session.class.getClassLoader() );
        try
        {
            message.setContent(multiPart);
            //transport.sendMessage(message, message.getAllRecipients());
            Transport.send(message, message.getAllRecipients());
        }
        catch (MessagingException e)
        {
            e.printStackTrace();
        }
        finally
        {
            Thread.currentThread().setContextClassLoader(classLoader);
        }
        transport.close();
    }
    catch (MessagingException mex)
    {
        mex.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

您的代码中有几个common mistakes。首先修复它们。

但是你的问题很可能是由于某种类加载器问题。请告诉我们有关您的程序运行环境的更多信息。