Solved
J2ME SMTP / POP3 Connection
Posted on 2004-08-15
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,