MessagingException:类型"多部分/混合"的MIME部分包含java.io.File类型的对象而不是MimeMultipart

时间:2017-06-09 15:57:55

标签: java javamail mime-types

我尝试发送带附件的电子邮件(pdf或doc(x))。这是我的方法。当我调用file.getFile()时 - 它返回一个对象java.io.File类型

public boolean sendEmail(Form form) {
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    try {
        MimeMessage message = javaMailSender.createMimeMessage();
        message.setContent(form.getFile(),"multipart/mixed");
        javaMailSender.send(message);
    } catch (MessagingException e) {
        log.error(e.getMessage(), e);
    }
    return false;
}

我在这里得到一个错误:

org.springframework.mail.MailSendException; nested exceptions (1) are:_Failed message 1: javax.mail.MessagingException: MIME part of type "multipart/mixed" contains object of type java.io.File instead of MimeMultipart [Sanitized]
    at com.my.sender.Sender.sendEmail(Sender.java:37)
    at com.spring.controller.SpringController.update(SpringController.java:133)
    at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:71)
    at com.liferay.portlet.CheckboxParametersPortletFilter.doFilter(CheckboxParametersPortletFilter.java:57)
    at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:68)
    at com.liferay.portlet.CheckboxParametersPortletFilter.doFilter(CheckboxParametersPortletFilter.java:57)
    at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:68)
    at com.liferay.portlet.CheckboxParametersPortletFilter.doFilter(CheckboxParametersPortletFilter.java:57)
    at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:68)
    at com.liferay.portlet.CheckboxParametersPortletFilter.doFilter(CheckboxParametersPortletFilter.java:57)
    at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:68)
    at com.liferay.portlet.CheckboxParametersPortletFilter.doFilter(CheckboxParametersPortletFilter.java:57)
    at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:68)
    at com.liferay.portlet.CheckboxParametersPortletFilter.doFilter(CheckboxParametersPortletFilter.java:57)
    at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:68)
    at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:48)
    at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:105)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at com.liferay.portal.osgi.web.wab.extender.internal.adapter.ServletExceptionAdapter.service(ServletExceptionAdapter.java:68)
    at org.eclipse.equinox.http.servlet.internal.registration.EndpointRegistration.service(EndpointRegistration.java:153)

如何解决?

1 个答案:

答案 0 :(得分:0)

JavaMail常见问题解答描述了how to create a message with an attachmentsendfile.java sample program就是一个完整的例子。