MIME类型为multipart / mixed的无对象DCH

时间:2019-02-27 21:13:24

标签: java apache email playframework freemarker

我正在使用Play Framework和apache通用电子邮件+ freemarker开发应用程序。 使用此方法,我遇到了一个问题,每当我发送电子邮件时,都会收到以下错误消息:

  

javax.mail.MessagingException:发送消息时发生IOException;   嵌套的异常是:javax.activation.UnsupportedDataTypeException:否   MIME类型为multipart / mixed的对象DCH

这是电子邮件堆栈:

package service.email;


import com.google.common.base.Strings;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.ImageHtmlEmail;
import play.Logger;
import utils.ConfigurationUtils;
import utils.enums.EmailTemplates;

import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import static java.util.Objects.nonNull;
import static utils.enums.ConfigurationKey.*;

@Singleton
public class EmailService {

  @Inject
  private MarkerService markerService;
  @Inject
  private ConfigurationUtils configurationUtils;

  private ImageHtmlEmail email;
  private final Configuration freemarkerConfiguration;
  private final String templatePrefixPath;
  private final String from;
  private final String overrideTo;

  @Inject
  public EmailService(ConfigurationUtils configurationUtils) {
    this.configurationUtils = configurationUtils;
    freemarkerConfiguration = new Configuration();

    freemarkerConfiguration.setDefaultEncoding("UTF-8");
    freemarkerConfiguration.setLocale(Locale.FRANCE);
    freemarkerConfiguration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

    templatePrefixPath = configurationUtils.getString(EMAIL_TEMPLATES_PATH);
    from = configurationUtils.getString(EMAIL_FROM);
    overrideTo = configurationUtils.getString(EMAIL_OVERRIDE_TO);


  }

  /**
   * @param to
   * @param emailTemplate
   * @param emailAttachments
   * @return CompletionStage<Void>
   * @throws EmailException
   * @throws IOException
   * @throws TemplateException
   */
  public CompletionStage<Void> send(String to, EmailTemplates emailTemplate, Map<String, String> datas,
                                    List<EmailAttachment> emailAttachments) {
    try {
      Logger.info("[EmailService] Building email :\n\tSent to {}\n\tTemplate used : {}\n", to, emailTemplate.getFileName());
      ImageHtmlEmail email = new ImageHtmlEmail();
      email.setHostName(configurationUtils.getString(EMAIL_HOSTNAME));
      email.setSmtpPort(configurationUtils.getInt(EMAIL_SMPT_PORT));
      email.setAuthenticator(new DefaultAuthenticator(configurationUtils.getString(EMAIL_USERNAME), configurationUtils.getString(EMAIL_PASSWORD)));
      email.setSSLOnConnect(configurationUtils.getBoolean(EMAIL_SSL_ENABLED));
      // This is useful in dev mode, you can redirect all emails to a single recipient by supplying the 'to' attribute
      email.addTo(Strings.isNullOrEmpty(overrideTo) ? to : overrideTo);
      email.setFrom(from);
      email.setSubject(emailTemplate.getSubject());

      Logger.info("[EmailService] Preparing freemarker binding...");
      Template template = freemarkerConfiguration.getTemplate(templatePrefixPath + emailTemplate.getFileName());
      Writer stringWriter = new StringWriter();
      template.process(datas, stringWriter);
      stringWriter.flush();
      stringWriter.close();
      email.setHtmlMsg(stringWriter.toString());
      email.setTextMsg("Your email client does not support HTML messages");
      Logger.info("[EmailService] attaching files...");
      if (nonNull(emailAttachments)) {
        for (EmailAttachment emailAttachment : emailAttachments) {
          email.attach(emailAttachment);
        }
      }
      Logger.info("[EmailService] Sending email...");
      email.send();
    } catch (EmailException | IOException | TemplateException e) {
      // TODO : Manage exception by type
      e.printStackTrace();
      Logger.debug("Error While sending email...\n");
    }
    return CompletableFuture.completedFuture(null);
  }

  public CompletionStage<Void> send(String to, EmailTemplates emailTemplate, Map<String, String> datas) {
    return send(to, emailTemplate, datas, null);
  }
}

模板:

<html>
<head>
    <title>Test</title>
</head>
<body>
 ${URL_RESET_PASSWORD}
</body>
</html>

这个问题已经困扰我一个星期了……我真的不明白为什么会出现错误,我只知道DCH为空,DCH Factory也为空。

1 个答案:

答案 0 :(得分:0)

我终于找到的解决方案:

  CompletableFuture.runAsync(() -> {
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    try {
      email.send();
    } catch (final EmailException e) {
      throw new RuntimeException(e);
    }
  }, Executors.newSingleThreadExecutor());

dch为空,因为当前的类加载器为空。