Retrieving email content with javaMail. Weird issue..

Ok so i have been searching google and other threads for hours trying to get this issue figured out.
Basically i have been making a program that send emails and text messages and also lets you check your inbox for unread messages you may have. I cant seem to get the content of an unread message to print out if it was sent by someone not sending it with my program.

This is the message i get printed out instead of the message body (“javax.mail.internet.MimeMultipart@176e552”)
I do not get any errors, it just prints out that message as if it was the body of the email.
What i dont understand is that im not sending an attachment with the email so why is it doing this?, The only think i can think of is that some emails have links in them and it thinks the links are attachments?

Again it only does this when it reading from an email that was sent from the email providers website itself, not my program.

Here is the code i have for when it reads the message.

			try
			{
				Properties prop = new Properties();
				prop.setProperty("mail.pop3.socketFactory.class",
						"javax.net.ssl.SSLSocketFactory");
				prop.setProperty("mail.pop3.socketFactory.fallback", "false");
				prop.setProperty("mail.pop3.port", s.popPort);
				prop.setProperty("mail.pop3.socketFactory.port", s.popPort);

				prop.put("mail.pop3.host", host);
				prop.put("mail.store.protocol", "pop3s");
				Session session = Session.getDefaultInstance(prop);
				store = session.getStore();
				System.out.println("your ID is : " + username);
				SimpleTextSender.textArea.setText("Connecting...");
				try
				{
					store.connect(host, username, password);
				} catch (AuthenticationFailedException e)
				{
					JOptionPane.showMessageDialog(null, "" + e.getMessage());
				}
				SimpleTextSender.textArea.setText("Connected...");
				Folder inbox = store.getDefaultFolder().getFolder("INBOX");
				inbox.open(Folder.READ_ONLY);

				Message[] msg = inbox.getMessages();
				for (int i = 0; i < msg.length; i++)
				{
					int answer = JOptionPane
							.showConfirmDialog(null, "Open message titled '"
									+ msg[i].getSubject() + "'?");
					if (answer == 0)
					{
						handleText(msg[i]);
					} 
					else if (answer == 1)
					{
						break;
					} 
					else
					{
						// i = msg.length+1;
						break;
					}

				}
				if (inbox != null)
				{
					inbox.close(true);
				}
				if (store != null)
				{
					store.close();
				}
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	private void handle(Message msg) throws Exception
	{
		JOptionPane.showMessageDialog(null, "From: "
				+ msg.getFrom()[0].toString() + "\n" + "Sent: "
				+ msg.getSentDate() + "\n");
	}

	private void handleText(Message msg) throws Exception
	{
		//handle(msg);
		//String messageText = msg.getContent().toString();
		SimpleTextSender.textArea.setText(msg.getContent().toString());
	}

It’s because your mail is MIME encoded in multiple parts. For instance it could contain HTML and plain text and propably attachments too.
You need to detect if it’s MIME and then decode it into the different parts.
Dunno if it’s in the API as I’m at work and can’t look it up, but it’s probably there :slight_smile:

Hope it helps :slight_smile:

/Ozak

So your ‘body’ is a reference to a javax.mail.internet.MimeMultipart instance.

Just cast it, and call the available methods to retreive your content.

Do you think you could possibly give me a small example of what exactly you mean?
I think i understand what you mean but i dont exactly know how to cast to that.

Ok so i got it to print this out.

<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
</head>
<body class='hmmessage'>
This is a test message i am sending to test my program!!!!!
 		 	   		  
<hr />Windows 7: Unclutter your desktop. <a href='http://www.microsoft.com/windows/windows-7/videos-tours.aspx?h=7sec&slideid=1&media=aero-shake-7second&listid=1&stop=1&ocid=PID24727::T:WLMTAGL:ON:WL:en-US:WWL_WIN_7secdemo:122009' target='_new'>Learn more.</a></body>
</html>

And i need to string parse the actual message out of it i believe but i have no clue how to do string parsing.

If you open it in swing’s JEditorPane (which by default reads html AFAIK) it should just show you the message.

By the way, your program sounds cool. I tried to make a JavaWebStart-able stand-alone email sending program once but couldn’t find a way to do it without messing around with lots of config files and other things.

I have no idea how to do the webstart stuff, Right i just create a executable jar file for it. the only problem is the html issue.

he told you hot to fix that. You just have to make a JEditorPane, and it will read the html for you, and output hte email.

Ok sorry for that, I’ll look up JEditorPanes, I have never used them before, but they dont sound very difficult so ill check them out.