Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

J2ME SMTP / POP3 Connection

Posted on 2004-08-15
5
Medium Priority
?
2,227 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 2000 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

The Ideal Solution for Multi-Display Applications

Check out ATEN’s VS1912 12-Port DP Video Wall Media Player at InfoComm 2017. Kerri describes how easy it is to design creative video walls in asymmetric layouts and schedule detailed playlists ahead of time with its advanced scheduling feature.

Question has a verified solution.

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

Tired of waiting for your show or movie to load?  Are buffering issues a constant problem with your internet connection?  Check this article out to see if these simple adjustments are the solution for you.
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…

730 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