×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    Sending mail from j2me too googlemail.com

    hi,

    i am new to j2me and.

    i am trying to send mail from j2me with the following codes to myAccountIngooglemail@googlemail.com.

    Code:
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    import javax.microedition.io.*;
    import java.io.*;
    
    public class EmailMidlet extends MIDlet implements CommandListener {
        Display display = null;
    
        // email form fields     
        TextField toField = null;
        TextField subjectField = null;
        TextField msgField = null;
        Form form;
    
        String url = "http://localhost:8080/C:/workspace/sendmail2/src/EmailServlet";
        static final Command sendCommand = new Command("send", Command.OK, 2);
        static final Command clearCommand = new Command("clear", Command.STOP, 3);
    
        String to;
        String subject;
        String msg;
    
    
        public EmailMidlet() {
           display = Display.getDisplay(this);
           toField = new TextField("To:", "", 25, TextField.EMAILADDR);
           subjectField = new TextField("Subject:", "", 15, TextField.ANY);
           msgField = new TextField("MsgBody:", "", 90, TextField.ANY);
           form = new Form("Fill Form");
    
        }
    
        public void startApp() throws MIDletStateChangeException {
          form.append(toField);
          form.append(subjectField);
          form.append(msgField);
          form.addCommand(clearCommand);
          form.addCommand(sendCommand);
          form.setCommandListener(this);
          display.setCurrent(form);
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
          notifyDestroyed();
        }
    
    
        void invokeServlet(String url) throws IOException {
            HttpConnection c = null;
            InputStream is = null;
            OutputStream os = null;
            StringBuffer b = new StringBuffer();
            TextBox t = null;
            try {
              c = (HttpConnection)Connector.open(url);
              c.setRequestMethod(HttpConnection.POST);
              c.setRequestProperty("IF-Modified-Since", "20 Oct 2001 16:19:14 GMT");
              c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
              c.setRequestProperty("Content-Language", "en-CA");
              c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
              
              os = c.openOutputStream();
              // encode data
              os.write(("to="+to).getBytes());
              os.write(("&subject="+subject).getBytes());
              os.write(("&msg="+msg).getBytes());
              os.flush();
    
              is = c.openDataInputStream();
              int ch;
              while ((ch = is.read()) != -1) {
                b.append((char) ch);
                System.out.print((char)ch);
              }
              t = new TextBox("Confirmation", b.toString(), 1024, 0);
              t.setCommandListener(this);
            } finally {
               if(is!= null) {
                  is.close();
               }
               if(os != null) {
                  os.close();
               }
               if(c != null) {
                  c.close();
               }
            }
            display.setCurrent(t);
        } 
    
       public void commandAction(Command c, Displayable d) {
          String label = c.getLabel();
          if(label.equals("clear")) {
            destroyApp(true);
          } else if (label.equals("send")) {
             to = toField.getString();
             subject = subjectField.getString();
             msg = msgField.getString();
             try {
               invokeServlet(url);
             }catch(IOException e) {}
          } 
       }
    }
    Code:
    import java.io.*;
    import java.io.DataOutputStream;
    import java.net.*;
    import java.util.Date;
    
    public class EmailSender {
    
      public EmailSender() {
      }
    
      public void send(String from, String to, String subject, String msg) {
         Socket smtpSocket = null; 
         DataOutputStream os = null;
         DataInputStream is = null;
    
         try {
           smtpSocket = new Socket("smtp.googlemail.com", 587);
           os = new DataOutputStream(smtpSocket.getOutputStream());
           is = new DataInputStream(smtpSocket.getInputStream());
         } catch (UnknownHostException e) {
           System.err.println("Don't know about host: hostname");
         } catch (IOException e) {
           System.err.println("Couldn't get I/O for the connection to: hostname");
         }
    
         if (smtpSocket != null && os != null && is != null) {
          try {
          
            os.write(("HELO there"+"\r\n").getBytes()); 
            os.write(("MAIL FROM: "+ from +"\r\n").getBytes());
            os.write(("RCPT TO: "+ to + "\r\n").getBytes());
            os.write(("DATA\r\n").getBytes());
            os.write(("Date: "+ new Date() + "\r\n").getBytes()); // stamp the msg with date
            os.write(("From: "+from+"\r\n").getBytes());
            os.write(("To: "+to+"\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());
            // debugging
            String responseLine;
            BufferedReader d
            = new BufferedReader(new InputStreamReader(is));
    
            while ((responseLine = d.readLine()) != null) {
              System.out.println("Server: " + responseLine);
              if (responseLine.indexOf("delivery") != -1) {
                 break;
              }
            }
    
            os.close();
            d.close();
            is.close();
            smtpSocket.close(); 
          } catch (UnknownHostException e) {
            System.err.println("Trying to connect to unknown host: " + e);
          } catch (IOException e) {
            System.err.println("IOException: " + e);
          }
        }
      } 
    }
    Code:
    import java.io.*;
    import java.text.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class EmailServlet extends HttpServlet {
    
       public void doPost(HttpServletRequest request,
                          HttpServletResponse response)
            throws IOException, ServletException
        {
            response.setContentType("text/plain");
            PrintWriter out = response.getWriter();
    
            String to = request.getParameter("to");
            String subject = request.getParameter("subject");
            String msg = request.getParameter("msg");
            // construct an instance of EmailSender
            EmailSender es = new EmailSender();
            es.send("myAccountInGooglemail@googlemail.com", to, subject, msg);
            out.println("mail sent....");
    
        }
    }
    My Quistions:
    1.In Class EmailMidlet: ist the url-path right?: String url = "http://localhost:8080/C:/workspace/sendmail2/src/EmailServlet";

    EmailServer ist located in my Computer in C:/workspace/sendmail2/src/EmailServlet.

    2.In the class EmailSender: is this right in case of googlemail? smtpSocket = new Socket("smtp.googlemail.com", 587);


    i am using wireless toolkit 2.5.2 and i have downloaded the codes from the internet.


    I would be very grateful if somebody can help me.


    thanx and best regards

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Sending mail from j2me too googlemail.com

    1.In Class EmailMidlet: ist the url-path right?
    How is your webserver set up? It would be unusual for the server you let you execute any path you like.

    2.In the class EmailSender: is this right in case of googlemail?
    Seems right to me. Usually, SMTP works on port 25, not 587. But Googlemail will accept 587.

    One step at a time... can you run the servlet from a web browser?

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    Re: Sending mail from j2me too googlemail.com

    hi grahamhughes,

    thanx for the answer..

    I am using xampp and the smtp server is active.

    I can run EmailServlet, when i type https://localhost/xampp/EmailServlet.java in firefox browser.

    I tried to run this too but it with does not work


    on the desplay ask:
    sendmail midlet suite wants to connect https://localhost/xampp/EmailServlet.java . this may result in charges.
    is it ok to use airtime?

    when i click yes, it does not do anything.
    Last edited by thedaystar; 2008-12-21 at 21:22.

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Sending mail from j2me too googlemail.com

    I can run EmailServlet, when i type file:///C:/workspace/sendmail2/src/EmailServlet.java in firefox browser.
    You can run it using the .java file?

    In any case, you need to use the http:// url like you're using in the MIDlet. No good accessing it through a file:// url because you bypass the web server.

    First step, make sure the servlet will actually run. You could create an HTML form so you can post test data to the servlet. Debug one program at a time.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    Re: Sending mail from j2me too googlemail.com

    Quote Originally Posted by thedaystar View Post
    hi grahamhughes,

    thanx for the answer..

    I am using xampp and the smtp server is active.

    I can run EmailServlet, when i type https://localhost/xampp/EmailServlet.java in firefox browser.

    I tried to run this too but it with does not work


    on the desplay ask:
    sendmail midlet suite wants to connect https://localhost/xampp/EmailServlet.java . this may result in charges.
    is it ok to use airtime?

    when i click yes, it does not do anything.
    sorry but i have edited the msg

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    Re: Sending mail from j2me too googlemail.com

    Quote Originally Posted by grahamhughes View Post
    You can run it using the .java file?
    Yes.

    i can open the file https://localhost/xampp/EmailServlet.java with firefox

  7. #7
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Sending mail from j2me too googlemail.com

    OK... use the same url for both your testing and your application.

    Can you send email through the servlet, from your browser?

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    Re: Sending mail from j2me too googlemail.com

    Quote Originally Posted by grahamhughes View Post

    Can you send email through the servlet, from your browser?
    Sending email through the servlet from the browser??How does it work?

  9. #9
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Sending mail from j2me too googlemail.com

    You servlet receives information from the phone as an HTTP POST, url-encoded. This is the format used by an HTML <form>. You can create a web-page, with a form, and use it to send information to your servlet. In this way, you can test the servlet separately from the midlet.

    It is much easier to test and fix one part at a time.

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    Re: Sending mail from j2me too googlemail.com

    hi,

    now i have tried to run my servlet separately from midlet. when i call the servlet form the browser it sty running and showing "waiting for local host"on the browser and i got this on the eclipse console:

    Server: 220 mx.google.com ESMTP p17sm15408381fka.10
    Server: 250 mx.google.com at your service


    i tried HelloWorldServlet and it was ok but EmailServlet

  11. #11
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Sending mail from j2me too googlemail.com

    OK... then your next job is to fix the servlet. *Then* you can start playing with the midlet.

    My guess would be that your problem is that you are just sending a sequence of commands to the SMTP server, without waiting for responses. Do not send anything to the server until you have a message from it telling you that it is ready. Send one command at a time, waiting for responses in between. Remember that some responses could be errors, in which case you might not want to continue sending.

    This is perhaps not the best forum to get help on writing a Java servlet that sends email...

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    5

    Re: Sending mail from j2me too googlemail.com


  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    Re: Sending mail from j2me too googlemail.com

    I have one quistion about transfering data between midlet-class servlet-class through DataInputStream and DataOutPutStream.

    i write the following in the midlet-class:
    DataOutputStream os = c.openDataOutputStream();
    os.write(("to="+to).getBytes()); (where String to="myemail@googlemail.com"; )
    os.write(("&subject="+subject).getBytes());
    .....
    ....

    and tried to call this in the servlet-class through the following:
    request.getParameter("to");

    it returns null

  14. #14
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Sending mail from j2me too googlemail.com

    Well... never use getBytes(). And you may need to URLencode the data. As a rule, for example, you shouldn't send "@" characters and the like.

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    Re: Sending mail from j2me too googlemail.com

    OK i solved the problem of to=null by deleting os.flush(); from the midlet-class

    now i am trying to find the IOException in the send(....)-method.

    i wrote main()method in the class EmailSender and called send()method

    with eclipse debug.

    the pointer jump from the line:
    Code:
    while ((responseLine = is.readLine()) != null) {....
    to:
    Code:
    catch (IOException e) {
    System.err.println("IOException: " + e.getMessage());
    e.printStackTrace();
    }
    and returns the following error:

    IOException: Software caused connection abort: recv failed

    java.net.SocketException: Software caused connection abort: recv failed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.io.DataInputStream.readLine(Unknown Source)
    at EmailSender.send(EmailSender.java:59)
    at EmailSender.main(EmailSender.java:79)

    i was researching about this error in google some people say firewall could be the problem.
    i shut down my firewall and antivirus but nothing change.

    any idea, what can i do?

    thank you

Similar Threads

  1. RTP in j2me for sending audio
    By encryptor in forum Mobile Java General
    Replies: 1
    Last Post: 2008-06-11, 12:23
  2. How can the WAP browser communicate to J2ME application?
    By hbfornies in forum Mobile Java General
    Replies: 20
    Last Post: 2007-03-02, 16:32
  3. J2ME file sending via bluetooth from PC to mobile
    By danishsk in forum Mobile Java General
    Replies: 2
    Last Post: 2007-02-12, 12:56
  4. Sending file(s) from PC to S60 mobiles using J2ME
    By alam.sher in forum Bluetooth Technology
    Replies: 1
    Last Post: 2006-12-22, 14:10
  5. Sending and receiving SMS messages from J2ME application
    By nkn_motoko in forum Mobile Java General
    Replies: 1
    Last Post: 2001-11-06, 20:34

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×