Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Retrieving email content with javaMail. Weird issue..  (Read 1438 times)
0 Members and 1 Guest are viewing this topic.
Offline Shane75776
« Posted 2009-12-03 02:26:32 »

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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
         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());
   }

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline ozak

Junior Devvie




Java demon has no respect for bad code!!!


« Reply #1 - Posted 2009-12-03 11:10:02 »

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 Smiley

Hope it helps Smiley

/Ozak

Want to do multiplayer? Why not try my free Socky socket server?
It's scriptable through JavaScript!
Check it out at http://www.furi.dk/socky
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2009-12-03 12:48:19 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Shane75776
« Reply #3 - Posted 2009-12-04 01:13:56 »

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.


Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Shane75776
« Reply #4 - Posted 2009-12-04 04:14:14 »

Ok so i got it to print this out.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
<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!!!!!<br>                      <br /><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.

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline CommanderKeith
« Reply #5 - Posted 2009-12-04 05:16:48 »

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.

Offline Shane75776
« Reply #6 - Posted 2009-12-04 06:46:29 »

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.

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #7 - Posted 2009-12-04 11:46:55 »

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.
Offline Shane75776
« Reply #8 - Posted 2009-12-04 19:46:15 »

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.

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (66 views)
2014-11-26 15:22:04

toopeicgaming1999 (58 views)
2014-11-26 15:20:36

toopeicgaming1999 (12 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (28 views)
2014-11-25 11:26:43

Gibbo3771 (24 views)
2014-11-24 19:59:16

trollwarrior1 (37 views)
2014-11-22 12:13:56

xFryIx (76 views)
2014-11-13 12:34:49

digdugdiggy (53 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!