You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSO2微集成器类中介发送带附件邮件异常及无内容问题

WSO2微集成器类中介发送带附件邮件异常问题

问题现象

无附件时邮件发送正常,添加附件后抛出以下异常:

javax.mail.MessagingException: IOException while sending message;
  nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;

环境信息

  • WSO2 Micro Integrator版本:4.1.0
  • 部署方式:类中介以Jar包形式部署在MI_HOME/lib目录下
  • 依赖Jar包:
    • javax.mail-1.6.2.jar
    • activation-1.1.1.jar
  • JDK版本:11.0.8

发送带附件邮件的Java代码

emailHostSMTPs = getConfig("email.host.smtps");
emailHostSMTP = getConfig("email.host.smtp");
emailProtocol = getConfig("email.protocol");
senderEmail = getConfig("email.sender");
senderPassword = Encryptor.decrypt(getConfig("email.password"));
senderUser = getConfig("email.sender.user");
emailPort = getConfig("email.port");

MimeMultipart multipart = new MimeMultipart();
Authenticator authenticator = new SMTPAuthenticator();
MailSSLSocketFactory sslSocketFactory = new MailSSLSocketFactory();
MimeBodyPart bodyPart = new MimeBodyPart();

String html = "";

Properties props = System.getProperties();

props.put("mail.transport.protocol", emailProtocol);
props.put("mail.smtp.host", emailHostSMTP);
props.put("mail." + emailProtocol + ".auth", "true");
props.put("mail.smtp.port", emailPort);

sslSocketFactory.setTrustAllHosts(true);
props.put("mail.smtp.ssl.socketFactory", sslSocketFactory);

Session session = Session.getInstance(props, authenticator);

html = "<html><body dir='rtl' style='font-size:12pt;color:#000000;background-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;'> "
        + emailBody + " </body></html>";
bodyPart.setContent(html, "text/html; charset=UTF-8");
multipart.addBodyPart(bodyPart);

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(senderEmail, fromDisplayName));
message.setReplyTo(new Address[] { new InternetAddress(fromEmail) });

if (toList != null && toList.size() > 0) {
    for (String to : toList) {
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    }
} else {
    throw new Exception("List of users to send email to is empty");
}

if (ccList != null && ccList.size() > 0) {
    for (String cc : ccList) {
        message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
    }
}

if (bccList != null && bccList.size() > 0) {
    for (String bcc : bccList) {
        message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
    }
}


MimeBodyPart mimeBodyPart = new PreencodedMimeBodyPart("base64");
mimeBodyPart.setContent(fileDataStrBase64, "application/*");
mimeBodyPart.setFileName(MimeUtility.encodeText(fileName, "utf-8", "B"));
multipart.addBodyPart(mimeBodyPart);


message.setSubject(subject, "UTF-8");
message.setContent(multipart);
message.setSentDate(new Date());

Transport transport = session.getTransport(emailProtocol);
transport.connect(senderEmail, senderPassword);
transport.sendMessage(message, message.getAllRecipients());

编辑补充

添加以下代码后,邮件能发送,但仅显示主题,无邮件内容及附件:

MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
mc.addMailcap("multipart/report;;  x-java-content-handler=com.sun.mail.dsn.multipart_report");
mc.addMailcap("message/delivery-status;; x-java-content-handler=com.sun.mail.dsn.message_deliverystatus");
mc.addMailcap("message/disposition-notification;; x-java-content-handler=com.sun.mail.dsn.message_dispositionnotification");
mc.addMailcap("text/rfc822-headers;;   x-java-content-handler=com.sun.mail.dsn.text_rfc822headers");

邮件调试信息

DEBUG: failed to load any providers, using defaults
DEBUG: Tables of loaded providers from javamail.providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle,1.6.2], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle,1.6.2], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle,1.6.2], com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle,1.6.2], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle,1.6.2], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle,1.6.2]}
DEBUG: Providers Listed By Protocol: {imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle,1.6.2], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle,1.6.2], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle,1.6.2], imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle,1.6.2], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle,1.6.2], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle,1.6.2]}
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle,1.6.2]
DEBUG SMTP: need username and password for authentication
DEBUG SMTP: protocolConnect returning false, host=mail.mydomain.com, user=myemail, password=<null>
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "mail.mydomain.com", port 587, isSSL false
220 MyMailServer Microsoft ESMTP MAIL Service ready at Sun, 28 May 2023 14:33:31 +0300
DEBUG SMTP: connected to host "mail.mydomain.com", port: 587
EHLO host.docker.internal
250-MyMailServer Hello [10.x.x.x]
250-SIZE 41943040
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-AUTH GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250 CHUNKING
DEBUG SMTP: Found extension "SIZE", arg "41943040"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "AUTH", arg "GSSAPI NTLM"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
STARTTLS
220 2.0.0 SMTP server ready
EHLO host.docker.internal
250-MyMailServer Hello [10.x.x.x]
250-SIZE 41943040
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-AUTH GSSAPI NTLM LOGIN
250-8BITMIME
250-BINARYMIME
250 CHUNKING
DEBUG SMTP: Found extension "SIZE", arg "41943040"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "AUTH", arg "GSSAPI NTLM LOGIN"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: protocolConnect login, host=mail.mydomain.com, user=mysenderemail, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2 
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<mysenderemail@mydomin.com>
250 2.1.0 Sender OK
RCPT TO:<myemail@mydomin.com>
250 2.1.5 Recipient OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   myemail@mydomin.com
DATA
354 Start mail input; end with <CRLF>.<CRLF>
Date: Sun, 28 May 2023 14:33:31 +0300 (AST)
From: mysenderemail@mydomin.com
To: myemail@mydomin.com
Message-ID: <1628804451.7.1685273611450@host.docker.internal>
Subject: test attachment localhost 2.24
MIME-Version: 1.0
Content-Type: multipart/mixed; 
    boundary="----=_Part_6_2147206188.1685273611445"

.
250 2.6.0 <1628804451.7.1685273611450@host.docker.internal> [InternalId=91379724192809, Hostname=MyMailServer2] 1339 bytes in 0.293, 4.462 KB/sec Queued mail for delivery
DEBUG SMTP: message successfully delivered to mail server
QUIT
221 2.0.0 Service closing transmission channel

内容的提问来源于stack exchange,提问作者Mahmoud Saleh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 20:53:08