Solved

J2ME SMTP / POP3 Connection

Posted on 2004-08-15
5
2,197 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article I will describe how to setup a Cisco WLC 5508 to work with Apple's Bonjour protocol across VLANs.  I will also discuss using screen mirroring and Airplay on an AppleTV v3.  This article covers the wireless network only and requires m…
Using in-flight Wi-Fi when you travel? Business travelers beware! In-flight Wi-Fi networks could rip the door right off your digital privacy portal. That’s no joke either, as it might also provide a convenient entrance for bad threat actors.
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…

809 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