Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default [Error] Reading mails and replying at the same time

    Hello All

    I am developing an application in which i m reading the mails from a gmail account using imap.gmail.com . The code is reading perfectly fine .

    Now my code when finds a new email it reply back the mail using javamail
    and for sending mail i m making a session smtp.gmail.com
    and mailing the desired message back to the sender .

    Now the logics good to me but i knew i will be having trouble with the session as the session i took during reading will not allow me to have a session to send the mails to the sender in the same code .

    And when i executed it is reading the emails perfectly but when the mail is sending i get an Exception

    Java Code:
    Access to default session denied
    So plz help me in this and one thing

    is my suggestion correct that when i find a new mail i stop the current code and run the sending program in this session overlapping problem will not be there but it is untidy and not efficient suggestion so plz help me :)
    Thanks in adv :)

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all, please post the complete exception you comes with.

    Secondly, you should implement these two process in different threads, rather in one. I cannot say much about your session without looking at what you've done, so better to post at-least the relevant code block here to see.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all, please post the complete exception you comes with.

    Secondly, you should implement these two process in different threads, rather in one. I cannot say much about your session without looking at what you've done, so better to post at-least the relevant code block here to see.

  4. #4
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    this is the only line i m getting as exception
    and yes i m trying to use 2 threads let me try that first but i dnt think it is efficient enough and i have a doubt over session even using 2 different thread let me try and i will get back to u soon :) thanks for your reply :)

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Use of multiple threads not totally support you to solve the problem. But it could gain the efficiency in your application. Looking at your exception I wonder how did you get that only one line. Did you customize the exception, can't you print the complete trace of it?

  6. #6
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    hi

    i m trying the thread method but unfortunately i m not able to close the thread which is reading the mails so i can not proceed to send the mail plz help this is my code

    and the output i m getting is

    Java Code:
    Reading value in run 1
    Reading
    imaps://gtbitinfo%40gmail.com@imap.gmail.com
    saved details in id.ravs
    calling close
    In close
    length of a is 2
    Thread stopped
    Thread stopped
    Reading value 0
    imaps://gtbitinfo%40gmail.com@imap.gmail.com
    imaps://gtbitinfo%40gmail.com@imap.gmail.com
    imaps://gtbitinfo%40gmail.com@imap.gmail.com
    after
    Reading value 0
    it shud show the messages read but it is showing imaps:// ... that means it is still reading the mails

    help plzz

    thank you in advance :)
    Last edited by ravjot28; 09-23-2010 at 11:54 AM.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  8. #8
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    I don't think that's your exact code, because you would get a NullPointerException here,

    Java Code:
    t=null;
    System.out.println("Thread stopped");
    reading=0;
    System.out.println("Reading value "+reading);
    t.stop();  //<-- null pointer

    Your code also doesn't make any sense: You create a thread called "t". This thread creates an InboxReader and starts reading. It then calls close() whereupon it tries to stop itself? Or perhaps the InboxReader's constructor creates a new Thread that starts reading. This thread then calls close() whereupon it tries to stop an already stopped thread?
    Last edited by Maxideon; 09-02-2010 at 03:46 PM.

  9. #9
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    sorry for giving the wrong code
    my code was

    and i have tested inboxreader it reads my mails correctly as u can see the length of mails was 2 and i had 2 new mails so it is all perfect for reading stuff
    but the thread is not finishing and not switching to displaying the mails info

    plz help thank u :) for the replies
    Last edited by ravjot28; 09-23-2010 at 11:54 AM.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    That's what I want to confirm too. In Close() method, nothing doing, just print some lines and close-up threads anonymously.

  11. #11
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    so kindly give me the way to stop the thread
    i tried this.t.stop but then my code gets terminated so kindly help me in stopping the thread and changin the value of reading and again start the thread it will be a gr8 help to me

    thanks in advance

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    There is nothing called thread stopping, which is an unsafe to do. Safest way is to rack the status of the thread.

    Look at the following code.

    Java Code:
        private Thread th;
    
        public void stop() {
            th= null;
        }
    
        public void run() {
            Thread thisThread = Thread.currentThread();
            while (th== thisThread) {
                try {
                    // process
                } 
                catch (Exception ex){
                }
            }
        }
    As you can see if you want to stop the thread call the stop() and change the status. Alternatively the advantage here is any time you can start the same thread. Really not starting, you are using the same.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  14. #14
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    Thanks Eranga now my threads stops and everything is fine related to thread
    but according to you i tried this thread method but unfortunately when the code runs it gives me same exception "Access to default session denied"
    I decided to post my whole code and the output so that you can get perfect picture

    output to this when i executed the code [NOTE : I had 2 mails in my inbox unread]

    Java Code:
    run:
    Reading value in run 1
    Reading
    imaps://gtbitinfo%40gmail.com@imap.gmail.com
    saved details in id.ravs
    calling close
    Sleep
    called close
    Reading value in run 0
    Sending
    FROM: "Sender email id",
    TO: "My email id"@gmail.com,
    SUBJECT: hi
    SendDate: Sat Aug 28 13:24:14 IST 2010
    "Sender email id"
    Access to default session denied
    FROM: "Sender email id,
    TO: "My email id",
    SUBJECT: Hi
    SendDate: Thu Sep 02 01:05:18 IST 2010
    "Sender email id"
    Access to default session denied
    FROM: "Sender email id",
    TO: "My email id"@gmail.com,
    SUBJECT: Coz i , walked the streets alone
    SendDate: Sun Sep 05 11:50:02 IST 2010
    "Sender email id"
    Access to default session denied
    Reading value in run 1
    Reading
    imaps://"my emailid"%40gmail.com@imap.gmail.com
    BUILD STOPPED (total time: 21 seconds)

    please help having no clue how to solve this problem as i had only one method that was via threading but it went in vain

    Help needed thanks in advance

    and thank you for ur replies :)
    Last edited by ravjot28; 09-23-2010 at 11:55 AM.

  15. #15
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    plzzzz help ppl :)

    thnk u in adv :)

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  17. #17
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    Now i printed all the exception in sending class and now my output is with the exception

    Java Code:
    Reading value in run 1
    Reading
    
    imaps://xyz%40gmail.com@imap.gmail.com
    
    saved details in id.ravs
    calling close
    Sleep
    called close
    Reading value in run 0
    
    Sending
    
    FROM: "" <emailid>,
    TO: [email]xyz@gmail.com[/email],
    SUBJECT: hi
    SendDate: Sat Aug 28 13:24:14 IST 2010
    emailid
    
    Error in sending java.lang.SecurityException: Access to default session denied
    Access to default session denied
    
    FROM: "" <emailid>,
    TO: [email]xyz@gmail.com[/email],
    SUBJECT: Hi
    SendDate: Thu Sep 02 01:05:18 IST 2010
    "" <emailid>,
    Error in sending java.lang.SecurityException: Access to default session denied
    Access to default session denied
    
    FROM: "" <emailid>,
    TO: [email]xyz@gmail.com[/email],
    SUBJECT: Coz i , walked the streets alone
    SendDate: Sun Sep 05 11:50:02 IST 2010
    emailid
    Error in sending java.lang.SecurityException: Access to default session denied
    Access to default session denied
    
    Reading value in run 1
    
    Reading
    
    imaps://xyz%40gmail.com@imap.gmail.com
    

    this is the exception plz help thnx :)
    Last edited by ravjot28; 09-13-2010 at 11:41 AM.

  18. #18
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    Please put printStackTrace() also to find the exact line also.
    Ramya:cool:

  19. #19
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    hi i now replaced println to printStack
    and got the output

    Java Code:
            at javax.mail.Session.getDefaultInstance(Session.java:316)
            at sending.sendSSLMessage(sending.java:63)
            at sending.send(sending.java:34)
            at Main.run(Main.java:84)
            at java.lang.Thread.run(Thread.java:637)
    java.lang.SecurityException: Access to default session denied
            at javax.mail.Session.getDefaultInstance(Session.java:316)
            at sending.sendSSLMessage(sending.java:63)
            at sending.send(sending.java:34)
            at Main.run(Main.java:84)
            at java.lang.Thread.run(Thread.java:637)
    java.lang.SecurityException: Access to default session denied
            at javax.mail.Session.getDefaultInstance(Session.java:316)
            at sending.sendSSLMessage(sending.java:63)
            at sending.send(sending.java:34)
            at Main.run(Main.java:84)
            at java.lang.Thread.run(Thread.java:637)
    plz help :) thnx in adv :)

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Page 1 of 2 12 LastLast

Similar Threads

  1. Help. Java run time error
    By slimce in forum New To Java
    Replies: 7
    Last Post: 05-15-2010, 04:15 PM
  2. sending mails thru pop3 server.........
    By vrk in forum Advanced Java
    Replies: 1
    Last Post: 03-25-2009, 10:59 PM
  3. Java 1.5 compile time error
    By ank_k in forum New To Java
    Replies: 4
    Last Post: 11-13-2008, 11:12 AM
  4. Replies: 21
    Last Post: 08-01-2008, 11:13 AM
  5. Forwarding mails in message/rfc822
    By prithviraj in forum Advanced Java
    Replies: 0
    Last Post: 05-18-2008, 10:30 AM

Posting Permissions

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