Search code examples
javajakarta-mail

Java Mail Incorrect message body


I am sending a mail using Java Mail api. but it seems working differently in different modules. when I invoke the method sendmail from module A, I receive mail as expected. but when I invoke the same method from module B, I receive the message with incorrect formatting. here is my code

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;
    }

The code from where I am invoking sendmail method from both modules A and B.

@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("[email protected]", "documents failed to post", msgBody, "failedMEssage.txt",attachmentText);

    }

Received mail from Module A enter image description here

Received mail from Module B enter image description here


Solution

  • the issue is resolved. there was an issue with maven dependency. in my module B there was org.apache.axis2 dependency used which has another sub dependency of javax.mail with diff. version. I excluded that dependency. as below; which resolved the issue.

        <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>