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 This email address is being protected from spambots. You need JavaScript enabled to view it.\r\n").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: This email address is being protected from spambots. You need JavaScript enabled to view it.\r\n").getBytes());
    os.write(("RCPT TO: This email address is being protected from spambots. You need JavaScript enabled to view it.\r\n").getBytes());
    os.write("DATA\r\n".getBytes());
    os.write(("From: This email address is being protected from spambots. You need JavaScript enabled to view it.\r\n").getBytes());
    os.write(("To: This email address is being protected from spambots. You need JavaScript enabled to view it.\r\n").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){}

  }