This tip demonstrates a method of sending a mail from a handset. Wireless portals support many of the same applications as Internet portals. For example they provide messaging services, which includes electronic mail. So this tip in J2ME may be very helpful for developers who want to implement these type of services.
void sendMail()
{
SocketConnection sc=null;
InputStream is=null;
OutputStream os=null;
//Login to mail server
try {
byte b[]=new byte[200];
int total=0;
sc = (SocketConnection)
Connector.open("socket://"+servAdd+":110");
is = sc.openInputStream();
os = sc.openOutputStream();
os.write(("USER sender@domain.com This email address is being protected from spam bots, you need Javascript enabled to view it ").getBytes());
total=is.read(b);
response+=new String(b,0,total);
os.write(("PASS 123\r\n").getBytes());
total=is.read(b);
response+=new String(b,0,total);
// Sending mail
sc = (SocketConnection)Connector.open("socket://"+serverAdd+":25");
is = sc.openInputStream();
os = sc.openOutputStream();
os.write(("HELO there" + "\r\n").getBytes());
os.write(("MAIL FROM: sender@domain.com This email address is being protected from spam bots, you need Javascript enabled to view it ").getBytes());
os.write(("RCPT TO: receiver@domain.com This email address is being protected from spam bots, you need Javascript enabled to view it ").getBytes());
os.write("DATA\r\n".getBytes());
os.write(("From: sender@domain.com This email address is being protected from spam bots, you need Javascript enabled to view it ").getBytes());
os.write(("To: receiver@domain.com This email address is being protected from spam bots, you need Javascript enabled to view it ").getBytes());
os.write(("Subject: "+subject+"\r\n").getBytes());
os.write((msg+"\r\n").getBytes()); // message body
os.write(".\r\n".getBytes());
os.write("QUIT\r\n".getBytes());
} catch(Exception e){}
}