I am trying to get the Content of an email, but the only thing I get is an empty String "".
I saved an Outlook 2013 e-mail directly by drag and drop in a Folder and try to get its Content with this method.
In addition there is definitely content in the email.
public static String getTextContentFromMail2(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 text 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 = getTextContentFromMail2(bp);
}
continue;
}
else if (bp.isMimeType("text/html"))
{
String s = getTextContentFromMail2(bp);
if (s != null)
{
return s;
}
}
else
{
return getTextContentFromMail2(bp);
}
}
return text;
}
else if (p.isMimeType("multipart/*"))
{
Multipart mp = (Multipart) p.getContent();
for (int i = 0; i < mp.getCount(); i++)
{
String s = getTextContentFromMail2(mp.getBodyPart(i));
if (s != null)
{
return s;
}
}
}
return null;
}
Just to make sure a sample Content of an email:
Von: Hello Hello [mailto:hello.hello#hello.hello.com] Im Auftrag von Hello-Hello-Hello.Hello
Gesendet: Donnerstag, 20. August 2015 10:33
An: Hello Hello
Betreff: WG: Hello -Hello -Hello Hello (FROM: - Hello Hello)
Mit freundlichen Grüßen / with kind regards / 此致
Hello .-Hello . Hello Hello
Related
I am working with a block of code that contains multiple lines of appended text. Is there a simple method to change the font color and size? I am specifically looking to change the font size of the appended text like sb.append(record.getSegText())?
Below is the code.
#Override
public String inspect(ReferencedCoordinate coord) throws VizException {
if (resourceData.hideSampling) {
return "";
}
// check if we are in the last frame
boolean lastFrame = false;
FramesInfo framesInfo = this.descriptor.getFramesInfo();
if (time != null) {
try {
Point point = gf.createPoint(coord.asLatLon());
for (String key : entryMap.keySet()) {
WarningEntry entry = entryMap.get(key);
AbstractWarningRecord record = entry.record;
if (matchesFrame(entry, time, framePeriod, lastFrame)
&& record.getGeometry() != null) {
Geometry recordGeom = record.getGeometry();
for (int i = 0; i < recordGeom.getNumGeometries(); i++) {
PreparedGeometry prepGeom = pgf.create(recordGeom
.getGeometryN(i));
if (prepGeom.contains(point)) {
StringBuffer sb = new StringBuffer();
String[] textToPrint = getText(record, 0);
for (String text : textToPrint) {
if (sb.length() > 0) {
sb.append(" ");
}
sb.append("\n\n\n\n\n\n");
sb.append(text);
sb.append(record.getSegText());
}
return sb.toString();
}
}
}
}
}
return "NO DATA";
}
This question is related to Java Mail API and Gmail Account.
I would like to display the message part from the gmail emails by ignoring the attachment file. My code is working properly in case of no attachment, but when it comes to email with attachment, it is not giving output.
I just want the message body from the email to be displayed.
Thanks in advance.
This is only first shot without trying to debug your code, but you could try to follow original Oracle suggestion:
private boolean textIsHtml = false;
/**
* Return the primary text content of the message.
*/
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 text 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);
continue;
} else if (bp.isMimeType("text/html")) {
String s = getText(bp);
if (s != null)
return s;
} else {
return getText(bp);
}
}
return text;
} else if (p.isMimeType("multipart/*")) {
Multipart mp = (Multipart)p.getContent();
for (int i = 0; i < mp.getCount(); i++) {
String s = getText(mp.getBodyPart(i));
if (s != null)
return s;
}
}
return null;
}
I think there is problem you are trying to get MultiPart and content from it.
The code above work only when :
You can call the getText method with a Message object (which is a
Part).
I am unable to retrieve the content of the body of "multipart/MIXED" mails ...
This is what I use to reading mails ....
private String read(Message message) throws MessagingException, IOException {
String result = message.getContentType().toString() + " Unable to read";
if (message instanceof MimeMessage) {
MimeMessage m = (MimeMessage) message;
Object contentObject = m.getContent();
if (contentObject instanceof Multipart) {
BodyPart clearTextPart = null;
BodyPart htmlTextPart = null;
Multipart content = (Multipart) contentObject;
int count = content.getCount();
for (int i = 0; i < count; i++) {
BodyPart part = content.getBodyPart(i);
if (part.isMimeType("text/plain")) {
clearTextPart = part;
break;
} else if (part.isMimeType("text/html")) {
htmlTextPart = part;
}
}
if (clearTextPart != null) {
result = (String) "<html><body>"
+ clearTextPart.getContent() + "</body></html>";
} else if (htmlTextPart != null) {
String html = (String) htmlTextPart.getContent();
result = android.text.Html.fromHtml(html).toString();
}
} else if (contentObject instanceof String) {
String html = (String) contentObject;
result = html;
} else {
result = "not found";
}
}
return result;
}
As you can see the problem is , the mail wont even pass the condition (message instanceof MimeMessage) ...
The output for "multipart/MIXED" mail is :
multipart/MIXED; boundary=20cf306..... Unable to read
public static String processBody( > Part p < , String operacao)
} else if (p.isMimeType("multipart/*")) {
*** MimeMessage m = (MimeMessage) p; ***
Multipart mp = (Multipart) m.getContent();
for (int i = 0; i < mp.getCount(); i++) {
t = processBody(mp.getBodyPart(i), operacao);
if (t != null)
return t;
}
}
You haven't provided enough detail. Certainly you can trace the flow of your program through that code. What exactly is happening? If it's failing one of the tests, what are the actual values you're seeing and what do you expect?
What version of JavaMail are you using?
Are you reading the message from a mail server? What does the debug output show?
When I'm reading email body from java.mail in contentText I get first plain text and after this HTML text. I.e. if send message is
<div><b>Mock</b><br />Mock 2</div>
contentText will contains:
Mock Mock
<div><b>Mock</b><br />Mock 2</div>
Below is my code to load contentText:
public void setContentText(Multipart multipart) throws MessagingException, IOException {
contentText ="";
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
getBodyToStringPart(bodyPart);
}
}
protected void getBodyToStringPart(BodyPart bodyPart) throws MessagingException, IOException {
String disposition = bodyPart.getDisposition();
if (!StringUtils.equalsIgnoreCase(disposition, "ATTACHMENT")) {
if (bodyPart.getContent() instanceof BASE64DecoderStream
&& bodyPart.getHeader("Content-ID") != null) {
BASE64DecoderStream base64DecoderStream = (BASE64DecoderStream) bodyPart
.getContent();
byte[] byteArray = IOUtils.toByteArray(base64DecoderStream);
byte[] encodeBase64 = Base64.encodeBase64(byteArray);
this.contentText = this.contentText.replaceAll(
"cid:"
+ bodyPart.getHeader("Content-ID")[0].replaceAll(">", "")
.replaceAll("<", ""), "data:" + bodyPart.getContentType()
+ ";base64," + new String(encodeBase64, "UTF-8"));
} else if (bodyPart.getContent() instanceof MimeMultipart) {
MimeMultipart mimeMultipart = (MimeMultipart) bodyPart.getContent();
for (int j = 0; j < mimeMultipart.getCount(); j++) {
getBodyToStringPart(mimeMultipart.getBodyPart(j));
}
} else {
this.contentText += bodyPart.getContent() + "";
}
} else {
// TODO: Do we need attachments ?
}
}
This JavaMail FAQ entry might help.
I am trying to get the contents of an html email including the tags etc. right now my code only returns the texts.this is my code:
Store store = session.getStore("pop3");
store.connect(host, username, passwoed);
Folder folder = store.getFolder("Inbox");
if (!folder.exists()) {
System.out.println("No INBOX...");
System.exit(0);
}
folder.open(Folder.READ_WRITE);
Message[] msg = folder.getMessages();
for (int i = msg.length - 1; i > 0; i--) {
String sent1 = df.format(sent);
sent1 = sent1.trim();
int index11 = sent1.indexOf(DateTime);
if (index11 != -1) {
String to = InternetAddress.toString(msg[i].getRecipients(Message.RecipientType.TO));
String s1 = "";
try {
Multipart multipart = (Multipart) msg[i].getContent();
for (int x = 0; x < multipart.getCount(); x++) {
BodyPart bodyPart = multipart.getBodyPart(x);
String disposition = bodyPart.getDisposition();
if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
DataHandler handler = bodyPart.getDataHandler();
s1 = (String) bodyPart.getContent();
} else {
s1 = (String) bodyPart.getContent();
}
}
}
}
any help would be appreciated.
You can find a mail with Content-Type: TEXT/HTML like this:
Object content = message.getContent();
if (content instanceof Multipart) {
Multipart mp = (Multipart) content;
for (int i = 0; i < mp.getCount(); i++) {
BodyPart bp = mp.getBodyPart(i);
if (Pattern
.compile(Pattern.quote("text/html"),
Pattern.CASE_INSENSITIVE)
.matcher(bp.getContentType()).find()) {
// found html part
System.out.println((String) bp.getContent());
} else {
// some other bodypart...
}
}
}
Output:
<H1>Hi there</H1><p>Bye.</p>