I have an email message that has contentType: TEXT/PLAIN; charset="=?utf-8?B?ICJVVEYtOCI=?="
What do I need to extractContent to eliminate the java.io.UnsupportedEncodingException: =?utf-8?B?ICJVVEYtOCI=?=
I have tried the following:
import java.io.IOException;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMultipart;
public class ExtractContentText
{
private static String extractContent(MimeMultipart mimeMultipartContent) throws MessagingException
{
String msgContentText = null;
Exception cause = null;
try
{
int numParts = mimeMultipartContent.getCount();
for (int partNum = 0; msgContentText == null
&& partNum < numParts; partNum++)
{
BodyPart part = mimeMultipartContent.getBodyPart(partNum);
System.out.println("BodyContent.PartNum: "
+ partNum + " has contentType: " + part.getContentType());
// TODO: Eliminate java.io.UnsupportedEncodingException: =?utf-8?B?ICJVVEYtOCI=?=
Object partContent = part.getContent();
if (partContent instanceof MimeMultipart)
{
try
{
System.out.println("Processing inner MimeMultipart");
msgContentText = extractContent((MimeMultipart) partContent);
System.out.println("Using content found in inner MimeMultipart");
}
catch (MessagingException e)
{
System.out.println("Ignoring failure while trying to extract message content for inner MimeMultipart: "
+ e.getMessage());
}
}
else
{
try
{
msgContentText = (String) part.getContent();
System.out.println("PartNum: "
+ partNum + " content [" + msgContentText + "]");
}
catch (ClassCastException e)
{
// If it is not a String, ignore the exception and continue looking
System.out.println("Ignoring Non-String message content: "
+ e.getMessage());
}
}
}
}
catch (MessagingException | IOException e)
{
cause = e;
System.out.println("Failure while trying to extract message content: "
+ e.getMessage());
}
finally
{
// Fail if content could not be extracted
if (msgContentText == null)
{
MessagingException ex;
if (cause == null)
{
ex = new MessagingException("Message content could not be extracted");
}
else
{
ex = new MessagingException("Message content could not be extracted - "
+ cause.getMessage(), cause);
}
System.out.println(ex);
throw ex;
}
}
return msgContentText;
}
public static void main(String[] args) throws MessagingException, IOException
{
Message m = null;
System.out.println(extractContent((MimeMultipart) m.getContent()));
}
}