• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
 
Why is the word "abbreviation" so long? And this ad is so short?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic