Search code examples
javaemailjakarta-mail

Send a table inside of RTF message JavaMail


I'm trying to send a email with a Table in string with RTF, but when I check the email the message body, the table lost the format, so I wondering what I'm doing wrong, this is the following chunk of code to send and email

public static void send(String asunto, String texto, String emailDestinatario){
    final String username = "[email protected]";
    final String password = "mypass";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse( emailDestinatario));
        message.setSubject(asunto);
        message.setText(texto);

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

what others configurations I need to send and email and recogniz me the format of table?

This is a document example to send via email

I get something like that(the table lost the format)

TARIFAS EMPLEADOS
TARIFA
IVA
TOTAL
EMPLEADOS HASTA $150.000.000
94,000
15,040
109,040
EMPLEADOS MAYOR DE $150.000.000
160,000
25,600
185,600

Solution

  • Have you tried something like :

        MimeMessage message = new MimeMessage(sesion);  
        .
        .
        .
        //Config your message....
    
        Multipart mp = new MimeMultipart();
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent("RTF HTML TEXT", "text/html");
    
        mp.addBodyPart(htmlPart);           
        message.setContent(mp);
    
        Transport.send(message);