La méthode JavaMail GetText () renvoie toujours NULL sur des emails HTML
Question
J'utilise la fonction de faq JavaMail GetText () Pour obtenir le corps principal du texte d'un courrier électronique spécifique sur un serveur IMAP.Voici la fonction 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;
}
Le débogage de la session IMAP revient:
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))
La solution
Lorsque vous reformaté le code, vous le cassez.Vous avez inséré des accolades dans les mauvais endroits.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow