发送带有MimeMultipart(附件文件和文本)和JBoss 6.3的MimeMessage

时间:2018-08-02 13:44:04

标签: java jboss javamail

我正在尝试在Java程序中发送带有附件的电子邮件。

但是我遇到了一个问题,当我通过JBoss服务器执行代码时,我会收到一封空邮件(没有任何HTML标记)。当我通过JUnit测试执行代码时。

我收到带有附件和正文的邮件。

使用相同的输入数据,相同的邮件服务器,我面临两种不同的行为。

当我不使用功能mail.setContent时,我会在2个环境(JBoss和JUnit test)中收到带有正文的邮件。

所以看来我的问题与setContent函数,MimeMultipart类型和JBoss执行有关。

我使用:

  • Java jdk1.7.0_79

  • Maven 3.3.9

  • JBoss EAP 6.3

  • javax.mail版本:1.6.0

这是我发送带有附件的邮件的代码:

    public void sendWithAttachment(ByteArrayOutputStream  attachmentByte, String filename) {


    Properties properties = new Properties();
    properties.setProperty("mail.smtp.host", "myhost");
    properties.setProperty("mail.smtp.port", "myport");


    try {
        final Address[] reply = {new InternetAddress("foo@test.fr")};

        final Session session = Session.getDefaultInstance(properties, null);

        final MimeMessage mail = new MimeMessage(session);

        String recipient = "foo-recipient@test.fr";
        String body = "test";
        mail.setFrom(new InternetAddress("foo@test.fr","foo@test.fr"));
        mail.setSubject("subject");
        mail.setReplyTo(reply);
        MimeMultipart multipart = generateAttachment(attachmentByte,filename,body);
        mail.setContent(multipart);

        final Transport transport = session.getTransport("smtp");
        transport.connect();

        mail.setRecipient(Message.RecipientType.TO, new InternetAddress(destinataire,destinataire));
        transport.sendMessage(mail, mail.getAllRecipients());
        transport.close();
    } catch (final MessagingException | UnsupportedEncodingException me) {
        me.printStackTrace();
    }
}

private MimeMultipart generateAttachment(ByteArrayOutputStream  attachmentByte, String filename, String body) throws MessagingException {
    MimeMultipart res = new MimeMultipart();
    byte[] poiBytes = attachmentByte.toByteArray();  

    DataSource dataSource = new ByteArrayDataSource(poiBytes, "application/octet-stream");
    BodyPart attachmentBodyPart = new MimeBodyPart();
    attachmentBodyPart.setDataHandler(new DataHandler(dataSource));
    attachmentBodyPart.setFileName(filename);

    BodyPart textBodyPart = new MimeBodyPart();
    textBodyPart.setText(body);
    textBodyPart.setContent(body, "text/html; charset=utf-8");

    res.addBodyPart(textBodyPart);
    res.addBodyPart(attachmentBodyPart);
    return res;
}

请问您有什么想法或线索解决我的问题吗?

谢谢

2 个答案:

答案 0 :(得分:1)

我知道这是一件小事,但我注意到您的端口和主机属性设置为相同。

Properties properties = new Properties();
properties.setProperty("mail.smtp.host", "myhost");
properties.setProperty("mail.smtp.host", "myport");

它们应该是

Properties properties = new Properties();
properties.setProperty("mail.smtp.host", "myhost");
properties.setProperty("mail.smtp.port", "myport");

我发现此页面https://www.journaldev.com/2532/javamail-example-send-mail-in-java-smtp最近运行POC时很有帮助。

答案 1 :(得分:0)

我找到了解决方案。 问题来自JBoss 6 javax库,它不同于myApp javax库。 我使用以下命令从我的应用程序修改了jboss-deployment-structure.xml文件:

    <exclusions>
        <module name="javaee.api"/>
        <module name="javax.mail.api"/>
    </exclusions>

然后加载所有外部jar:

 <module name="modules.my.app" export="true" services="import"
            slot="${framework.versionSlot}">
            <imports>
                <include path="**" />
            </imports>
</module>

现在,JBoss使用了良好的库,并且可以正常工作