Java邮件不正确的邮件正文

时间:2018-03-12 07:03:52

标签: java javamail

我正在使用Java Mail api发送邮件。但它似乎在不同的模块中有所不同。当我从模块A调用方法 sendmail 时,我按预期收到邮件。但是当我从模块B调用相同的方法时,我收到格式不正确的消息。这是我的代码

public void sendmail(String toAddress, String subject, String messageBody, String fileName, String attachmentContent) throws MDRException {
        try{
            log.debug(new LogEntry(EventType.PROCESS, EventStatus.IN_PROGRESS,  "Sending E-MAIL notification to :"+toAddress +" subject :"+ subject+" message :"+ messageBody));

            MimeMessage message = getMimeMessage(toAddress, subject);
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(messageBody,"text/html; charset=utf-8" );

            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            messageBodyPart = new MimeBodyPart();

            DataSource source = null;
            try {
                    source = new ByteArrayDataSource(attachmentContent, "text/plain");
                } catch (IOException e) {
                    log.error(new LogEntry(EventType.ADMINISTRATIVE, EventStatus.FAILURE,"Error occurred while creating email attachment"),e);
            }
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(fileName);
            multipart.addBodyPart(messageBodyPart);

            message.setContent(multipart);

            message.addHeader("Content-Transfer-Encoding", "8bit");

            Transport.send(message);

            log.debug(new LogEntry(EventType.PROCESS, EventStatus.IN_PROGRESS,  "E-Mail sent successfully to :"+toAddress +" subject :"+ subject+" message :"+ messageBody));
        }catch( MessagingException e){
            log.error(new LogEntry(EventType.ADMINISTRATIVE, EventStatus.FAILURE,  "Error while sending mail"),e);
            throw new MDRException(MDRError.SENDING_NOTIFICATION_ERROR, e);
        }
    }

    public MimeMessage getMimeMessage(String toAddress,String subject) throws MDRException{
        String to = toAddress;
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", smtpHost);
        Session session = Session.getDefaultInstance(properties);
        MimeMessage message = new MimeMessage(session);

        try{
            message.addHeader("format", "flowed");
            message.addHeader("Content-Transfer-Encoding", "8bit");
            message.setFrom(new InternetAddress(fromEmail));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);

        }catch( MessagingException e){
            log.error(new LogEntry(EventType.ADMINISTRATIVE, EventStatus.FAILURE,  "Error while creating mime message"),e);
            throw new MDRException(MDRError.SENDING_NOTIFICATION_ERROR, e);
        }
        return message;
    }

我从模块A和B调用sendmail方法的代码。

@Autowired
SendEmailNotification sendEmailNotification;
.
.
.
.

@Test
    public void testEmail() throws Exception {

        String msgBody = "<html><body><h1> CSV attachment message</h1></body></html>";
        String attachmentText = "Source Id, Metadata Text";//messageFormat1(docDetails);

        sendEmailNotification.sendmail("test@test.com", "documents failed to post", msgBody, "failedMEssage.txt",attachmentText);

    }

收到来自模块A的邮件 enter image description here

收到来自模块B的邮件 enter image description here

1 个答案:

答案 0 :(得分:0)

问题已解决。 maven依赖存在问题。在我的模块B中,使用了org.apache.axis2依赖项,它具有javax.mail与diff的另一个子依赖项。版。我排除了这种依赖性。如下;这解决了这个问题。

    <dependency>
        <groupId>org.apache.axis2</groupId>
        <artifactId>axis2-adb</artifactId>
        <version>1.5.1</version>
        <exclusions>
            <exclusion>  
              <groupId>javax.mail</groupId>
              <artifactId>mail</artifactId>
            </exclusion>
        </exclusions> 
    </dependency>