send mail code not work for java EE 5 web project

I am using myeclipse 7, where i have added java EE 5 libraries witch i required.If i add the mail.jar externally to the application, it calling the javaee.jar from java EE 5 libraries.It’s not using the mail.jar.If i remove java EE 5 libraries then it is working, But i require java EE 5 libraries. How can i send mail using javaee.jar of java EE 5 library?.

If it require below is my hava code for sed mail

 String  d_email = "[email protected]",
                d_password = "pass",
                d_host = "smtp.gmail.com",
                d_port  = "465",
                m_to = "[email protected]",
                m_subject = "Testing",
                m_text = "This is the testing email.";

        public Main()
        {
            Properties props = new Properties();
            props.put("mail.smtp.user", d_email);
            props.put("mail.smtp.host", d_host);
            props.put("mail.smtp.port", d_port);
            props.put("mail.smtp.starttls.enable","true");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.debug", "true");
            props.put("mail.smtp.socketFactory.port", d_port);
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "false");

            SecurityManager security = System.getSecurityManager();

            try
            {
                Authenticator auth = new SMTPAuthenticator();
                Session session = Session.getInstance(props, auth);
                session.setDebug(true);

                MimeMessage msg = new MimeMessage(session);
                msg.setText(m_text);
                msg.setSubject(m_subject);
                msg.setFrom(new InternetAddress(d_email));
                msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
                Transport.send(msg);

                System.out.println("Mail Sent");
            }
            catch (Exception mex)
            {
                mex.printStackTrace();
            } 
        }

    public static void main(String[] args)
    {
        Main blah = new Main();
    }

    private class SMTPAuthenticator extends javax.mail.Authenticator
    {
        public PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(d_email, d_password);
        }
    }

Error :

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
    at javax.mail.Session.loadProvidersFromStream(Session.java:928)
    at javax.mail.Session.access$000(Session.java:174)
    at javax.mail.Session$1.load(Session.java:870)
    at javax.mail.Session.loadResource(Session.java:1084)
    at javax.mail.Session.loadProviders(Session.java:889)
    at javax.mail.Session.<init>(Session.java:210)
    at javax.mail.Session.getInstance(Session.java:232)
    at com.ctn.origin.connection.Main.<init>(Main.java:35)
    at com.ctn.origin.connection.Main.main(Main.java:55)

Answer

You should not manually add container specific libraries like javaee.jar to your project. It would only lead to runtime troubles if you deploy and run the project on a container of a different make/version. The javaee.jar is specific to Glassfish. Since Glassfish by itself already ships with a mail.jar this suggests that your target runtime is not Glassfish at all. It’s probably Tomcat or JBoss or something.

If you did this to overcome project compilation errors, you should have solved this differently. I don’t do MyEclipse, but I believe this is much similar to Eclipse, so this Eclipse based answer should work out for MyEclipse as well: rightclick the project, choose Properties, go to Target Runtime section and select the target runtime (the servletcontainer which you’d like to use) from the list. This way Eclipse will automatically include its libraries in the project’s build path the right way.

See also:

Leave a Reply

Your email address will not be published. Required fields are marked *