mario-tank
mario-tank

Reputation: 21

Java mail. Reply message separated as message and attachment

text message that I set is attached as text file in the letter. I don't understand why it's happening.

replied letter example

public void sendEmail(MimeMessage message, String textMessage){
        Session session = getSession();
        Transport transport = null;
        BodyPart part = new MimeBodyPart();

        MimeMultipart multipart = new MimeMultipart();
        try {
            String recipients = InternetAddress.toString(message.getRecipients(Message.RecipientType.TO));             
            MimeMessage replyMessage = (MimeMessage) message.reply(false);
            replyMessage.setSubject("RE: " + message.getSubject());
            replyMessage.setFrom(new InternetAddress(APPROVER));
            replyMessage.setReplyTo(message.getReplyTo());

            replyMessage.addRecipients(Message.RecipientType.TO, recipients);

            part.setContent(message.getContent(), message.getContentType());
            multipart.addBodyPart(part);
            part = new MimeBodyPart();
            part.setText(textMessage);
            multipart.addBodyPart(part);
            replyMessage.setContent(multipart);

            transport = session.getTransport("smtp");
            transport.connect(SERVER_HOST, APPROVER, APPROVER_PASSWORD);
            transport.sendMessage(replyMessage, replyMessage.getAllRecipients());
        } catch (IOException|MessagingException e) {
            e.printStackTrace();
        }
    }

Upvotes: 2

Views: 2149

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29971

That's because you're attaching the original message first and the reply text after the original message. Most mailers are going to display the reply text as an attachment. Put the reply text first. Also, the original message is not normally added as an attachment to the reply. Often the text of the original message is included in the text of the reply. See this JavaMail FAQ entry for composing the reply text and this JavaMail FAQ entry for finding the main body text in the original message.

Upvotes: 1

Related Questions