J2ME SMTP / POP3 Connection

Hi all,

I've created a midlet which also should send and receive Emails via SocketConnections (I'm using a Sony Ericsson P900 with MIDP 2.0 and GPRS-Connection).
While sending and receiving emails works in the emulator, it doesn't on the mobile. It seems he doesn't leave the while-loop (Reading InputStream).
I send a quit command in both cases, but somehow the InputStream doesn't seem to end...i have no more ideas. Would be great if anyone could help me - would be urgent!
The code for sending and receiving emails is the following:

  public void sendEmail() {

     try {
       sc = (SocketConnection) Connector.open("socket://" + smtpServerAdress + ":25");
       is = sc.openInputStream();
       os = sc.openOutputStream();
       sb = new StringBuffer();
     
       // Send SMTP-Commands
       os.write(("HELO" + domain + "\r\n").getBytes());
       os.write(("MAIL FROM: "+ from_emailAdress +"\r\n").getBytes());
       os.write(("RCPT TO: "+ to_emailAdress + "\r\n").getBytes());
       os.write("DATA\r\n".getBytes());
       os.write(("Date: " + new Date() + "\r\n").getBytes());
       os.write(("From: "+ from_emailAdress +"\r\n").getBytes());
       os.write(("To: "+ to_emailAdress +"\r\n").getBytes());
       os.write(("Subject: MIAM TEST\r\n").getBytes());
       os.write(("TESTMESSAGE FROM MIAM \r\n").getBytes());
       os.write(".\r\n".getBytes());
       os.write("QUIT\r\n".getBytes());

       int c = 0;
     
       // DOESN'T LEAVE LOOP
       while (  (c = is.read() ) != -1)  {
          sb.append( (char)c);

     }

     System.out.println(sb.toString());
   }
   catch (Exception e) {
     e.printStackTrace();
   }

   finally {
        try {
           if(is != null) {
              is.close();
           }
           if(os != null) {
              os.close();
           }
           if(sc != null) {
              sc.close();
           }
        } catch(IOException e) {
           e.printStackTrace();
        }
     }

 }

  public void getEmail() {

    try {
      sc = (SocketConnection) Connector.open("socket://" + popServerAdress + ":110");
      is = sc.openInputStream();
      os = sc.openOutputStream();
      sb = new StringBuffer();

      int ch = 0;
      os.write( ("USER " + userName + "\r\n").getBytes());
      os.write( ("PASS " + userPass + "\r\n").getBytes());
      os.write( ("LIST" + "\r\n").getBytes());
      os.write( ("RETR 1" + "\r\n").getBytes());
      os.write("QUIT\r\n".getBytes());

      while ( (ch = is.read() ) != -1) {
           sb.append( (char) ch);
      }
        System.out.println(sb.toString());
    }

    catch (Exception e) {
      e.printStackTrace();

    }
    finally {
         try {
            if(is != null) {
               is.close();
            }
            if(os != null) {
               os.close();
            }
            if(sc != null) {
               sc.close();
            }
         } catch(IOException e) {
           e.printStackTrace();
         }
      }
  }

Regards,
muaddib33Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimmackCommented:
Are the reads/writes running within their own threads?

Are you sure that the loop is being reached (eg. does the output stream need flushing when the last data has been written)?
0
muaddib33Author Commented:
I think I solved the problem - it seems to have something to do with the StringBuffer, as the initial size is only 16 characters.

I added sb.ensureCapacity(100); - now it seems to work.
0
jimmackCommented:
;-)

Post a 0 point question in the Support topic area (http://www.experts-exchange.com/Community_Support/) asking for a points refund.
0
muaddib33Author Commented:
;)

By the way: Do you know what's the easiest way to measure the bytes sent and received when opening http-, smtp- and pop3-sessions?
0
jimmackCommented:
This is handset dependent and can't be monitored from the client side.  You'll need to write a small amount of test code on the server side to check the size of the headers that are included in the request.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Wireless Networking

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.