title: Convert content-type from text/plain to text/html in an email with attachment
body: <p>I'm trying to display email messages gotten from the server. Prior to letting you know my issue, take a look at my code:  </p>
  
  <pre><code>public FileMailExtended getSingleMessage(Integer fileMailId) throws MailSystemException, IOException,
          MessagingException, BOServiceException {
      FileMail email = retrieve(fileMailId.intValue());
  
      MailerScopeDef scope = email.getType();
      boolean outbound = email.getDirection() == EmailConstants.DIRECTION_SENT;
      Long uId = Long.valueOf(email.getUId());
  
      String folder = null;
      if (outbound) {
          folder = ConfigurationService.getSentFolder(scope);
      } else {
          folder = ConfigurationService.getReadFolder(scope);
      }
  
      FileMailConfig fileMailConfig = getFileMailConfig(folder);
      long uidValidity = 0;
      if (fileMailConfig != null) {
          uidValidity = fileMailConfig.getVersionId();
      }
  
      ImapFolderManager ifm;
      if (outbound) {
          ifm = ((ImapSender) ConfigurationService.getMailSender(scope)).getImapFolderMger();
      } else {
          ifm = ConfigurationService.getImapFolderManagerReader(scope);
      }
  
      FileMailExtended emailExtended = new FileMailExtended();
      mapFileMailToFileMailExtended(email, emailExtended);
      try {
          IMAPMessage message = (IMAPMessage) ifm.getMessage(uId, uidValidity);
          if (message == null) {
              LOGGER.error("Could not read message with uId:" + uId + " from the server.");
              return null;
          }
  
          InternetAddress intAdress = (InternetAddress) message.getSender();
          String unicodeString = intAdress.toUnicodeString().replace("\"", "");
          emailExtended.setFrom(unicodeString);
          emailExtended.setTo(appendAddresses(message.getRecipients(RecipientType.TO)));
          emailExtended.setCc(appendAddresses(message.getRecipients(RecipientType.CC)));
          emailExtended.setCci(appendAddresses(message.getRecipients(RecipientType.BCC)));
          emailExtended.setSubject(message.getSubject());
  
          String content = "";
  
          if (message.getContent() instanceof String) {
              content += (String) message.getContent();
          } else if (message.getContent() instanceof Multipart) {
              Multipart multipart = (Multipart) message.getContent();
              StringBuffer buf = new StringBuffer();
              for (int x = 0; x &lt; multipart.getCount(); x++) {
                  BodyPart bodyPart = multipart.getBodyPart(x);
                  if (bodyPart == null || bodyPart.getContentType() == null) {
                      continue;
                  }
                  if (bodyPart.isMimeType(TEXT_HTML)) {
                      buf.append((String) bodyPart.getContent());
                  } else if (bodyPart.getContent() instanceof Multipart) {
                      Multipart multipart2 = (Multipart) bodyPart.getContent();
                      for (int xx = 0; xx &lt; multipart2.getCount(); xx++) {
                          BodyPart bodyPart2 = multipart2.getBodyPart(xx);
                          if (bodyPart2 != null &amp;&amp; bodyPart2.isMimeType("text/html") {
                              buf.append((String) bodyPart2.getContent());
                          }
                      }
                  }
              }
              content = buf.toString();
          }
          emailExtended.setContent(content);
  
          if (message.getContent() instanceof Multipart) {
              Multipart multipart = (Multipart) message.getContent();
              for (int i = 0; i &lt; multipart.getCount(); i++) {
                  BodyPart bodyPart = multipart.getBodyPart(i);
                  if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
                      // dealing with attachments only
                      continue;
                  }
                  emailExtended.addAttachmentNames(bodyPart.getFileName());
              }
          }
  
      } finally {
          closeFolderManager(ifm);
      }
  
      return emailExtended;
  }
  </code></pre>
  
  <p>This code works fine when the email doesn't have any attachments. I get the emailContent, formatted in html, thus readable without efforts.<br>
  My problem is, once the email contains an attachment file, nothing is displayed.<br>
  I discovered that it was because of the content-type, which was <strong>"text/plain"</strong> when sending a message with attachments when "text/html" with simple mail.
  So I changed my condition to take in account that content-type, but the result displayed <em>is..uh..just raw text</em>.<br>
  I even planned to force the content-type to <strong>"text/html"</strong> with the code <code>bodyPart2.setHeader("Content-Type", "text/html");</code> , but it ended up with an exception. 
  Any clue why the content-type of email with attachments has to be <strong>"text/plain"</strong>? i already googled it, but no significant answers.<br>
  How can I change it?<br>
  Many thanks.</p>