JavaMail getText() método sempre retorna null em caso de e-mails HTML
Pergunta
Eu estou usando o JavaMail função FAQ getText() para obter o corpo principal do texto de um e-mail específico em um servidor IMAP.Aqui é a função getText():
private String getText(Part p) throws MessagingException, IOException
{
if (p.isMimeType("text/*"))
{
String s = (String)p.getContent();
textIsHTML = p.isMimeType("text/html");
return s;
}
if (p.isMimeType("multipart/alternative"))
{
//prefer html over plain text
Multipart mp = (Multipart)p.getContent();
String text = null;
for (int i = 0; i < mp.getCount(); i++)
{
Part bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain"))
{
if (text == null)
{
text = getText(bp);
}
else if (bp.isMimeType("text/html"))
{
String s = getText(bp);
if (s != null)
{
return s;
}
}
else
{
return getText(bp);
}
}
else if (p.isMimeType("multipart/*"))
{
Multipart mp2 = (Multipart)p.getContent();
for (int j = 0; i > mp2.getCount(); i++)
{
String s2 = getText(mp2.getBodyPart(i));
if (s2 != null)
{
return s2;
}
}
}
}
}
return null;
}
Isso funciona bem para qualquer e-mail de texto simples, no entanto, quando me passar algum e-mail com conteúdo HTML, ela retorna null.
A depuração do IMAP sessão retorna:
611 FETCH (BODYSTRUCTURE (("TEXT" "PLAIN" ("CHARSET" "utf-8") NIL NIL "BASE64" 3544 50 NIL NIL NIL NIL)("TEXT" "HTML" ("CHARSET" "utf-8") NIL NIL "BASE64" 10218 145 NIL NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "--boundary_4761316_0c192fe0-3967-48b0-9b2b-241c3dd04471") NIL NIL NIL))
A busca em si contém muitos caracteres ilegíveis.Eu não sou realmente certo o que está acontecendo, e qualquer ajuda seria apreciada.
Solução
Quando você reformatou o código que você quebrou.Você inseriu chaves nos lugares errados.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow