Solved

J2ME SMTP / POP3 Connection

Posted on 2004-08-15
5
2,216 Views
Last Modified: 2010-10-05
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,
0
Comment
Question by:muaddib33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 11808389
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
 

Author Comment

by:muaddib33
ID: 11808524
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
 
LVL 15

Expert Comment

by:jimmack
ID: 11815198
;-)

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

Author Comment

by:muaddib33
ID: 11817785
;)

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
 
LVL 15

Accepted Solution

by:
jimmack earned 500 total points
ID: 11820013
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

Featured Post

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

MAC Filtering: MAC filtering is like handing a list of names to a doorman. If someone comes to the door and mentions a name, this name is checked by the doorman on his list and granted or denied access by this. This means that if someone menti…
This paper addresses the security of Sennheiser DECT Contact Center and Office (CC&O) headsets. It describes the DECT security chain comprised of “Pairing”, “Per Call Authentication” and “Encryption”, which are all part of the standard DECT protocol.
This Micro Tutorial will show you how to maximize your wireless card to its maximum capability. This will be demonstrated using Intel(R) Centrino(R) Wireless-N 2230 wireless card on Windows 8 operating system.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question