Hi All,
I want to read the body message and attachment of mail. But when I start reading mail if the mail contains the attachment only attachment content is being read. I am not able to read the body message.
Below is the code.
public void readEmails()
{
Store store = null;
Folder folder = null;
try
{
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties, null);
store = session.getStore(PROTOCOL);
store.connect(HOST, USERNAME, USERPASSWORD);
folder = store.getFolder(PROCESSFOLDER);
if (folder == null && !folder.exists())
{
System.out.println("Invalid folder ");
}
folder.open(Folder.READ_WRITE);
Message[] messages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
// For Starting
for (int i = 0; i < messages.length; i++)
{
Message msg = messages[i];
String contentType = msg.getContentType();
String textMsg = "";
if (contentType.contains("text/html") || contentType.contains("text/plain"))
{
textMsg = msg.getContent().toString();
System.out.println("Text Msg 111::::::::: " + textMsg);
} else if (contentType.contains("multipart"))
{
Multipart multiPart = (Multipart) msg.getContent();
int partCount = multiPart.getCount();
System.out.println("Multipart Count::::::::::: "+partCount);
for (int j = 0; j < partCount; j++)
{
BodyPart part = multiPart.getBodyPart(j);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()))
{
System.out.println("Attachment data::::::: "+part.getFileName());
System.out.println("Attachment data::::::: "+part.getContent().toString());
} else
{
System.out.println(part.getContent()+"_______");
// not an attachment, maybe the body content of the message
textMsg = part.getContent() != null ? part.getContent().toString() : "";
System.out.println("Text Msg::::::::: " + textMsg);
}
}
}
}
} catch (Exception ex)
{
ex.printStackTrace();
}
}
Thanks
Pankaj singh
I want to read the body message and attachment of mail. But when I start reading mail if the mail contains the attachment only attachment content is being read. I am not able to read the body message.
Below is the code.
public void readEmails()
{
Store store = null;
Folder folder = null;
try
{
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties, null);
store = session.getStore(PROTOCOL);
store.connect(HOST, USERNAME, USERPASSWORD);
folder = store.getFolder(PROCESSFOLDER);
if (folder == null && !folder.exists())
{
System.out.println("Invalid folder ");
}
folder.open(Folder.READ_WRITE);
Message[] messages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
// For Starting
for (int i = 0; i < messages.length; i++)
{
Message msg = messages[i];
String contentType = msg.getContentType();
String textMsg = "";
if (contentType.contains("text/html") || contentType.contains("text/plain"))
{
textMsg = msg.getContent().toString();
System.out.println("Text Msg 111::::::::: " + textMsg);
} else if (contentType.contains("multipart"))
{
Multipart multiPart = (Multipart) msg.getContent();
int partCount = multiPart.getCount();
System.out.println("Multipart Count::::::::::: "+partCount);
for (int j = 0; j < partCount; j++)
{
BodyPart part = multiPart.getBodyPart(j);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()))
{
System.out.println("Attachment data::::::: "+part.getFileName());
System.out.println("Attachment data::::::: "+part.getContent().toString());
} else
{
System.out.println(part.getContent()+"_______");
// not an attachment, maybe the body content of the message
textMsg = part.getContent() != null ? part.getContent().toString() : "";
System.out.println("Text Msg::::::::: " + textMsg);
}
}
}
}
} catch (Exception ex)
{
ex.printStackTrace();
}
}
Thanks
Pankaj singh