• Post Reply Bookmark Topic Watch Topic
  • New Topic

javax.email and getting message body text of multipart msg...

 
ramon diego
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you read the message body into a string variable? I have been working with the javax.mail.message object and can't get access to the message body of any MultiPart email.

Thanks.
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your code for more information to solve this problem.


JavaMail Tutorial
 
ramon diego
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. I simply want to get the text of the message body of the email. The content of the message. Check this code out. I have commented where I want to grab the message body text.

Date nowDate = new Date();
System.out.println(nowDate + " ### Undeliverable Process Starting ###");
String originalSender = "";
try
{
boolean debug = false;

String protocol = EMailProperties.PROTOCOL;
String host = EMailProperties.HOST;
String user = EMailProperties.MAILBOX;
String password = EMailProperties.PASSWORD;
String mbox = EMailProperties.FOLDER;

// Get a Session object
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);

// Get a Store object
Store store = session.getStore(protocol);
store.connect(host, user, password);

// Open the Folder
Folder folder = store.getDefaultFolder();

if (folder == null)
{
System.out.println("No default folder");
System.exit(1);
}

folder = folder.getFolder(mbox);
if (folder == null)
{
System.out.println("Invalid folder");
System.exit(1);
}

folder.open(Folder.READ_WRITE);
int totalMessages = folder.getMessageCount();

int newMessages = folder.getNewMessageCount();

// Attributes & Flags for all messages ..
Message[] msgs = folder.getMessages();

// Use a suitable FetchProfile
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.FLAGS);
fp.add("X-Mailer");
folder.fetch(msgs, fp);

for (int i = 0; i < msgs.length; i++)
{
try
{
seen = false;

//I simply want to read the message content (message text) into a string variable here.
String testcontent = (MimeMessage)msgs[i].getContent();
originalSender = getParts(msgs[i]);
if(!seen)
invalidateEMail(originalSender);
}
catch (UnsupportedEncodingException uee)
{
// This occurs when message is Content-Type: text/plain; charset=unicode-1-1-utf-7
nowDate = new Date();
System.out.println(nowDate + " UnsupportedEncoding occurred... Skipping this message.");
}
catch (Exception e)
{
nowDate = new Date();
System.out.println(nowDate + " " + e.toString() + " occurred... Skipping this message.");
e.printStackTrace();
}
}

// Expunge folder (truly delete all messages flagged delete):
folder.close(true);
store.close();
}

catch (Exception e)
{
nowDate = new Date();
System.out.println(nowDate + " " + e.toString() + " occurred. Stopping Undeliverables processing.");
e.printStackTrace();
}
...
 
ramon diego
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link to the tutorial. The description for getting a messages content seems dubious. The getContent method does not print out anything resembling the message content that I can see. Rather it prints out some jibberish about the messages being multi part...

Tutorial Reads:

Once you have a Message to read, you can get its content with getContent() or write its content to a stream with writeTo(). The getContent() method only gets the message content, while writeTo() output includes headers.

System.out.println(((MimeMessage)message).getContent());
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!