JMS邮件服务器的字符编码问题

时间:2015-03-18 21:44:20

标签: java-ee jms mail-server

当内容带有重音字符时,我在使用JMS发送电子邮件时遇到问题。

消息被推送到队列并作为对象消息放置。侦听器侦听消息并将其传递给邮件服务器。

我的本​​地服务器上的一切正常。但是,当我在Solaris生产服务器上运行相同的时候,重音字符会变得混乱。在电子邮件内容中,它为DeLeón打印DeLe n。基本上,重音字符没有被编码并显示问号[ ]。

非常感谢您对此问题的任何帮助。下面是我用来传递消息的代码。

private void sendEmailMessage(EmailBean emailBean) {
    System.out.println("SendReceivedEmailMessages::sendEmail()");
    try {

        if (emailBean.getFromAddresses() != null && !emailBean.getFromAddresses().isEmpty()) {
            Properties existingProperties = session.getProperties();
            existingProperties.put("mail.from", emailBean.getFromAddresses());
            session = Session.getInstance(existingProperties);
        }

        String DELIMITER = ";";
        String subject = emailBean.getSubject();
        String msgText = emailBean.getBody();

        MimeMessage message = new MimeMessage(session);
        message.setSubject(subject);
        message.setContent(message, "text/html");


        InternetAddress[] toAddressList = parseRecipients(emailBean.getToAddresses(), DELIMITER);
        InternetAddress[] ccAddressList = parseRecipients(emailBean.getCcAddresses(), DELIMITER);
        InternetAddress[] bccAddressList = parseRecipients(emailBean.getBccAddresses(), DELIMITER);

        message.setRecipients(javax.mail.Message.RecipientType.TO, toAddressList);
        message.addRecipients(javax.mail.Message.RecipientType.CC, ccAddressList);
        message.addRecipients(javax.mail.Message.RecipientType.BCC, bccAddressList);

        Multipart multipart = new MimeMultipart();

        if (emailBean.getFileByteArrRegular() != null) {
            addAttachmentFiles(multipart, emailBean.getFileByteArrRegular(), emailBean.getFilename() + "_Regular.docx", msgText);
        }
        if (emailBean.getFileByteOthers() != null) {
            addAttachmentFiles(multipart, emailBean.getFileByteOthers(), emailBean.getFilename() + ".docx", msgText);
        } else {
            addAttachmentFiles(multipart, null, null, msgText);
        }

        message.setContent(multipart);
        Transport.send(message);
    } catch (MessagingException ex) {
        Logger.getLogger(SendReceiveEmailMessages.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        Logger.getLogger(SendReceiveEmailMessages.class.getName()).log(Level.SEVERE, ex.getStackTrace()[0].toString(), ex);
    } catch (Exception ex) {
        Logger.getLogger(SendReceiveEmailMessages.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        Logger.getLogger(SendReceiveEmailMessages.class.getName()).log(Level.SEVERE, ex.getStackTrace()[0].toString(), ex);
    }
}

1 个答案:

答案 0 :(得分:0)

以防它可能对其他人有所帮助:

message.setContent(message, "text/html; charset=UTF-8");

上述代码更改解决了我的问题。