1 Reply Latest reply on Mar 1, 2004 10:28 AM by Viet

    HTML mail in MailModule

    Per-Erik Lindskog Newbie

      Hi guys!

      I needed to send HTML formatted mail, so I patched the MailModule a little. If anybody else needs this, here is one way to do it.

      It checks if the first character in the body is a "<". If it is, it assumes this is html, otherwise it's text. A little crude, but it works.

      In the MailModule in the method deliver (int, String, String, String, String, String), replace the following code

       // Prepare message
       MimeMessage message = new MimeMessage(session);
       message.setFrom(new InternetAddress(from));
       message.setSubject(subject);
       message.setText(body);
       message.setSentDate(new Date());
       message.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);
      

      with this:
       // Prepare message
       MimeMessage message = new MimeMessage(session);
       message.setFrom(new InternetAddress(from));
       message.setSubject(subject);
       message.setSentDate(new Date());
       message.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);
      
       if (body != null && body.length() > 0 && body.substring(0, 1).equals("<")) {
       // Assume HTML
       BodyPart messageBodyPart = new MimeBodyPart();
       messageBodyPart.setContent(body, "text/html");
       MimeMultipart multipart = new MimeMultipart("related");
       multipart.addBodyPart(messageBodyPart);
       message.setContent(multipart);
       } else {
       // Default = text
       message.setText(body);
       }
      


      You will also need to add these extra imports:
      import javax.mail.BodyPart;
      import javax.mail.internet.MimeBodyPart;
      import javax.mail.internet.MimeMultipart;
      


      Good luck.

      brgds Per-Erik