Solved

J2ME SMTP / POP3 Connection

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

With the purchase of CloudCommand by Comcast customers are left in a bind as subscriptions expire and render the AP's disabled. The following will explain how to flash your Ubiquiti AP's with CloudCommand firmware back to Ubiquiti firmware. HOWTO…
Working settings for French ISP Orange "Prêt à Surfer" SIM cards for data connections only. Can't be found anywhere else !
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now