Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By impacted

Thread: Problem while sending a java mail

  1. #1
    dushyant1244 is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default Problem while sending a java mail

    Hi

    I am trying to send a mail using smtp server. The code is showing no error & it hangs after some time.

    private void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("Test is successful");

    String from = "No-reply@webmaster.com";

    GetConnection connection = new GetConnection();
    String to="";
    try{
    connection.getConnection();
    JiraDAOImpl dao = new JiraDAOImpl();
    to = dao.getEmail(request.getParameter("username"));
    }catch(Exception e){
    }

    System.out.println("value of to >>>" + to);

    String subject = "Time Spent Report Update";
    String message = "Please find the Time Spent Report attachment with this mail";

    Properties props = new Properties();
    props.setProperty("mail.smtp.host", "sendmtw.mindtree.com");
    props.setProperty("mail.smtp.port", "25");
    props.setProperty("mail.smtp.auth", "false");
    props.setProperty("mail.smtp.starttls.enable", "true");

    Session session = Session.getDefaultInstance(props);
    session.setDebug(true);

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    try {

    MimeMessage msg = new MimeMessage(session);
    System.out.println("got session");
    msg.setSubject(subject);
    msg.setFrom(new InternetAddress(from));
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    msg.setContent(message.toString(),"text/plain");
    System.out.println("msg>>>>>>>"+ msg);
    Transport.send(msg);

    String title = "Send Email";
    String res = "Sent message successfully";
    String docType =
    "<!doctype html public \"-//w3c//dtd html 4.0 " +
    "transitional//en\">\n";
    out.println(docType +
    "<html>\n" +
    "<head><title>" + title + "</title></head>\n" +
    "<body bgcolor=\"#f0f0f0\">\n" +
    "<h1 align=\"center\">" + title + "</h1>\n" +
    "<p align=\"center\">" + res + "</p>\n" +
    "</body></html>");

    } catch (MessagingException ex2) {
    ex2.printStackTrace();
    }
    }

    it shows debug logs as

    JDBCURL: jdbc:oracle:thin:@10.60.9.165:1522:keenan
    Username :ref
    strPassword:ref
    Drivername :oracle.jdbc.driver.OracleDriver
    JDBCURL: jdbc:oracle:thin:@10.60.9.165:1522:keenan
    Username :ref
    strPassword:ref
    Drivername :oracle.jdbc.driver.OracleDriver
    value of to >>>dushyant_lamyan@mindtree.com
    DEBUG: setDebug: JavaMail version 1.4.3
    got session
    DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
    DEBUG SMTP: useEhlo true, useAuth false
    DEBUG SMTP: trying to connect to host "sendmtw.mindtree.com", port 25, isSSL false
    220 ************************************************** ***********************************
    DEBUG SMTP: connected to host "sendmtw.mindtree.com", port: 25

    EHLO A4KEEMD08516.mindtree.com
    250-mtw02mlr01.mindtree.com Hello [10.60.9.208], pleased to meet you
    250-ENHANCEDSTATUSCODES
    250-PIPELINING
    250-8BITMIME
    250-SIZE
    250-DSN
    250-ETRN
    250-XXXXXXXXA
    250 XXXB
    DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
    DEBUG SMTP: Found extension "PIPELINING", arg ""
    DEBUG SMTP: Found extension "8BITMIME", arg ""
    DEBUG SMTP: Found extension "SIZE", arg ""
    DEBUG SMTP: Found extension "DSN", arg ""
    DEBUG SMTP: Found extension "ETRN", arg ""
    DEBUG SMTP: Found extension "XXXXXXXXA", arg ""
    DEBUG SMTP: Found extension "XXXB", arg ""
    DEBUG SMTP: use8bit false
    MAIL FROM:<No-reply@webmaster.com>
    MAIL FROM:<No-reply@webmaster.com>
    250 2.1.0 <No-reply@webmaster.com>... Sender ok
    RCPT TO:<dushyant_lamyan@mindtree.com>
    250 2.1.5 <dushyant_lamyan@mindtree.com>... Recipient ok
    DEBUG SMTP: Verified Addresses
    DEBUG SMTP: dushyant_lamyan@mindtree.com
    DATA
    354 Enter mail, end with "." on a line by itself
    QUIT




    Please help me in this issue.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    Java Code:
    try{
        connection.getConnection();
        JiraDAOImpl dao = new JiraDAOImpl();
        to = dao.getEmail(request.getParameter("username"));
    } catch(Exception e) {
    }
    That bit is nasty...eating an exception. What happens if the connection fails? Or the dao has problems? You'll never know.

    Java Code:
    System.out.println("got session");
    msg.setSubject(subject);
    msg.setFrom(new InternetAddress(from));
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    msg.setContent(message.toString(),"text/plain");
    System.out.println("msg>>>>>>>"+ msg);
    Going by your debug output the problem is in here, since the first sys out is printing, but the last one isn't. Nothing there that should pose a problem since all you seem to be doing is setting things, but I haven't done email stuff in years.

    Are you sure you're not eating more exceptions somewhere?

  3. #3
    dushyant1244 is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default RE

    Hi

    Thanks for reply.

    Actually this is a POC only. And i am sure that i am not eating anymore exception.
    I suspect some problem in session creation

    Session session = Session.getDefaultInstance(props);

    But i am not sure. I gone through some of the existing creations on net but none of answer is clear.

    As it is not showing the message part in the debug as it used to be

    for ex.
    DEBUG SMTP SENT: DATA
    DEBUG SMTP RCVD: 354 Enter mail, end with "." on a line by itself

    DEBUG SMTP SENT:
    .
    DEBUG SMTP RCVD: 250 2.0.0 k4UA9Wh1001734 Message accepted for delivery

    DEBUG SMTP SENT: QUIT


    if you see message delivery is present in logs but this thing is absent in my logs

    One more concern is that it is sending mail sucessfully 1 out of 25 times i am trying.

    Please take a look & suggest me something

    Thanks & Regards
    Dushyant

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    Have you stepped through it in a debugger to see exactly where it hangs?

  5. #5
    dushyant1244 is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default RE

    Hi

    Thanks for reply

    Yes i checked. It is hanging at

    Session session = Session.getDefaultInstance(props);

    after this is going to Daemon threads and showing the error in the page :::

    --------------------------------------------------------------------
    java.lang.ClassNotFoundException: javax.mail.StreamLoader
    org.apache.catalina.loader.WebappClassLoader.loadC lass(WebappClassLoader.java:1645)
    org.apache.catalina.loader.WebappClassLoader.loadC lass(WebappClassLoader.java:1491)
    java.lang.ClassLoader.loadClassInternal(Unknown Source)
    com.keenan.jira.emailReport.EmailServlet.process(E mailServlet.java:56)
    com.keenan.jira.emailReport.EmailServlet.doPost(Em ailServlet.java:93)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    ----------------------------------------------------------

    If you see the logs it is showing error at EmailServlet.java:56 which is

    Session session = Session.getDefaultInstance(props); line:56
    Thanks & Regards
    Dushyant

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    Have you got the correct jar file?
    Unless the class loader's playing silly buggers, that implies you are missing the JavaMail jar file.

  7. #7
    dushyant1244 is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default RE

    Hi

    Thanks for the reply

    i am using mail-1.4.3.jar and activation.jar currently. Are these compatible

    Apart of this i am using
    eclipse 3.5.0 (eclipse galileo) with tomcat apache-tomcat-6.0.29

    Please let me know if jar used are correct or not.

    Thanks & Reagrds
    Dushyant

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    No idea, but that error message is implying that Tomcat can't see the jar file.

  9. #9
    dushyant1244 is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default Re

    Hi

    Thanks fro reply

    But if i remove the jar from the lib folder it shows so many compilation error but if i adds the jar, all will go. it means it is picking up the jar.

    There may be the chance that i am not using the correct jar or incompatible jar. So i ll try the other versions of the jar (mail.jar) & let see if it works. If you will get anything else on this. Please reply

    Thanks & Regards
    Dushyant lamyan

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    That's compilation.
    The exception above is during runtime.
    At runtime Tomcat can't find that class, so presumably can't see that jar.
    Are you sure it's being deployed?

    I can't remember how the internal tomcat works for Eclipse, I always dropped the war onto my own Tomcat and attached the debugger to it.

  11. #11
    dushyant1244 is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default RE

    Hi

    Thanks for the reply.

    I am not using war deployment here. I am using the as a project tree structure. I added these two jars in the WebContent -> WEB-INF -> lib

    Thanks & Regards
    Dushyant Lamyan

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    This is why I do war deployment, so I don't have to wonder whether it's some odd setup in my IDE that's wrong...I can just look at the unzipped war and see if the correct libraries are in there or not.

  13. #13
    dushyant1244 is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    Hi

    Thanks for the reply

    I tried this option too. Same results.

    Thanks & Regards
    Dushyant Lamyan

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    That error means it cannot see the jar/find the class.

    Since I can't see your system, that is all I can tell you...so you'll have to hunt around in your setup to see if it's successfully deployed, referenced, whatever.

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,377
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dushyant1244 View Post
    i am using mail-1.4.3.jar and activation.jar currently. Are these compatible
    If you're using Java 1.6 you don't need JAF at all (it is included with the Java core distribution). Otherwise you need version 1.1.1. See this link.

    kind regards,

    Jos

  16. #16
    impacted is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default Re: RE

    Quote Originally Posted by dushyant1244 View Post
    Hi

    Thanks for reply

    Yes i checked. It is hanging at

    Session session = Session.getDefaultInstance(props);

    after this is going to Daemon threads and showing the error in the page :::

    --------------------------------------------------------------------
    java.lang.ClassNotFoundException: javax.mail.StreamLoader
    org.apache.catalina.loader.WebappClassLoader.loadC lass(WebappClassLoader.java:1645)
    org.apache.catalina.loader.WebappClassLoader.loadC lass(WebappClassLoader.java:1491)
    java.lang.ClassLoader.loadClassInternal(Unknown Source)
    com.keenan.jira.emailReport.EmailServlet.process(E mailServlet.java:56)
    com.keenan.jira.emailReport.EmailServlet.doPost(Em ailServlet.java:93)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    ----------------------------------------------------------

    If you see the logs it is showing error at EmailServlet.java:56 which is

    Session session = Session.getDefaultInstance(props); line:56
    Thanks & Regards
    Dushyant
    I know this post is really old, BUT I came across in during my search for a solution to this exact same problem. I was trying to use JavaMail in my application, and it was a very strange problem, because I followed the exact instructions step by step to add the library to my IDE, Eclipse (instructions found here: JavaMail API - FAQ, JavaBeans also covered right above it). I could execute the application perfectly from within Eclipse, but as soon as I exported it into a .jar it would simply hang, giving no errors or any indications as to what was wrong. I commented out the code which used the JavaMail libraries and sure enough it no longer hung. So I knew it had something to do with how I installed them.

    Well, after trying a lot of different things and doing a lot of searching, this guy's post actually helped guide me toward the answer. Our issues were identical. I finally looked at this part of the JavaMail FAQ, and it directed me the right way: JavaMail API - FAQ. This FAQ addresses the error he was getting. The pertinent part is: "add the Class-Path header to the manifest file of your jar file." So I went to the manifest guide (found here: http://docs.oracle.com/javase/tutori...r/downman.html), found instructions as to how to add this header, recompiled, and, as it states in the manifest article, you cannot reference an external .jar which has been sealed inside another .jar through the Class-Path header in the manifest. What that means is that after you have modified the manifest file to include mail.jar (the JavaMail library), you must make sure you have a copy of mail.jar in the working directory of the executing .jar which references it.

    This solved my problem entirely. I personally don't wish to distribute the mail.jar along with my .jar. The above guide on manifests states that you CAN make your .jar reference another .jar file which has been sealed inside it through custom coding, so I am going to take the time to learn how to do this, thus eliminating the need to also provide mail.jar along with my executable.

    Anyway, hope this ends up helping someone else along the way. Good luck.
    Last edited by impacted; 03-15-2013 at 10:39 PM. Reason: Added link to manifest guide
    SnakeDoc likes this.

Similar Threads

  1. Java Mail sending
    By jazz2k8 in forum Advanced Java
    Replies: 1
    Last Post: 05-01-2011, 06:08 AM
  2. problem with sending mail usin javaX.mail api
    By sandeepsai39 in forum New To Java
    Replies: 4
    Last Post: 11-25-2009, 05:37 AM
  3. Problem in sending mail from java
    By npoorni in forum Advanced Java
    Replies: 1
    Last Post: 06-30-2009, 04:09 PM
  4. Facing problem while sending e-mail
    By jay8186 in forum Java Servlet
    Replies: 1
    Last Post: 03-25-2009, 07:34 AM
  5. Sending a mail with the local mail program
    By thedude in forum Advanced Java
    Replies: 2
    Last Post: 07-23-2007, 12:19 PM

Tags for this Thread

Posting Permissions

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