0 Replies Latest reply on Aug 21, 2009 8:12 PM by socal_javaguy

    Trying to send e-mail using JavaMail, JBoss 5, and JNDI

      Hello there,

      Am using JBoss 5.1.0GA and JDK 1.5.0_19 on OS X Leopard.

      Created a working [SendMailServlet|http://tinyurl.com/SendMailServletCode].

      Have now decided to refactor it into two separate classes (extract out JavaMail code to a separate class and create a ServletController).

      Am also trying to use JNDI to access the connection properties in the mail-service.xml configuration file residing in JBoss.

      The Mailer class contains the reusable functionality needed to send an e-mail:

      public class Mailer {
       private Session mailSession;
      
       protected void sendMsg(String email, String subject, String body)
       throws MessagingException, NamingException {
       Properties props = new Properties();
       InitialContext ictx = new InitialContext(props);
       Session mailSession = (Session) ictx.lookup("java:/Mail");
      // Session mailSessoin = Session.getDefaultInstance(props);
       String username = (String) props.get("mail.smtps.user");
       String password = (String) props.get("mail.smtps.password");
      
       MimeMessage message = new MimeMessage(mailSession);
       message.setSubject(subject);
       message.setRecipients(javax.mail.Message.RecipientType.TO,
       javax.mail.internet.InternetAddress.parse(email, false));
       message.setText(body);
       message.saveChanges();
      
       Transport transport = mailSession.getTransport("smtps");
       try {
       transport.connect(username, password);
       transport.sendMessage(message, message.getAllRecipients());
       Logger.getLogger(this.getClass()).warn("Message sent");
       }
       finally {
       transport.close();
       }
       }
      }
      


      The MailController class serves as a standard Java Servlet which invokes the Mailer.class's sendMsg() method:

      public class MailController extends HttpServlet {
       /** static final HTML setting for content type */
       private static final String HTML = "text/html";
      
       myapp/** static final HTML setting for content type */
       private static final String PLAIN = "text/plain";
      
       public void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
       doPost(request, response);
       }
      
       public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
       response.setContentType(PLAIN);
       PrintWriter out = response.getWriter();
       String mailToken = TokenUtil.getEncryptedKey();
       String body = "Hello there, " + "\n\n"
       + "Wanna play a game of golf?" + "\n\n"
       + "Please confirm: https://localhost:8443/myapp/confirm?token="
       + mailToken + "\n\n" + "-Golf USA";
       Mailer mailer = new Mailer();
       try {
       mailer.sendMsg("recipient@gmail.com", "Golf Invitation!", body);
       out.println("Message Sent");
       }
       catch (MessagingException e) {
       e.printStackTrace();
       }
       catch (NamingException e) {
       e.printStackTrace();
       }
       }
      }
      


      Have the mail configuration set under $JBOSS_HOME/server/default/deploy/mail-service.xml:

      <server>
       <mbean code="org.jboss.mail.MailService" name="jboss:service=Mail">
       <attribute name="JNDIName">java:/Mail</attribute>
       <attribute name="User">user</attribute>
       <attribute name="Password">password</attribute>
       <attribute name="Configuration">
       <configuration>
       <property name="mail.store.protocol" value="pop3"/>
       <property name="mail.transport.protocol" value="smtp"/>
       <property name="mail.user" value="user"/>
       <property name="mail.pop3.host" value="pop3.gmail.com"/>
       <property name="mail.smtp.host" value="smtp.gmail.com"/>
       <property name="mail.smtp.port" value="25"/>
       <property name="mail.from" value="user@gmail.com"/>
       <property name="mail.debug" value="true"/>
       </configuration>
       </attribute>
       <depends>jboss:service=Naming</depends>
       </mbean>
      </server>
      


      web.xml (Deployment Descriptor):

      <servlet>
       <servlet-name>MailController</servlet-name>
       <servlet-class>com.myapp.MailController</servlet-class>
      </servlet>
      
      <servlet-mapping>
       <servlet-name>MailController</servlet-name>
       <url-pattern>/sendmail</url-pattern>
      </servlet-mapping>
      


      This is what is outputted when I start JBOSS and click point my browser to:

      https://localhost:8443/myapp/sendmail

      [MailService] Mail Service bound to java:/Mail
      
      [STDOUT] DEBUG: JavaMail version 1.4ea
      [STDOUT] DEBUG: java.io.FileNotFoundException:
      
      /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/javamail.providers
      (No such file or directory)
      
      [STDOUT] DEBUG: !anyLoaded
      [STDOUT] DEBUG: not loading resource: /META-INF/javamail.providers
      [STDOUT] DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
      
      [STDOUT] DEBUG: getProvider() returning
      javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
      
      [STDOUT] DEBUG SMTP: useEhlo true, useAuth false
      [STDOUT] DEBUG SMTP: trying to connect to host "localhost", port 465, isSSL true
      
      [STDERR] javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 465;
       nested exception is:
       java.net.ConnectException: Connection refused
      [STDERR] at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
      [STDERR] at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
      [STDERR] at javax.mail.Service.connect(Service.java:275)
      [STDERR] at javax.mail.Service.connect(Service.java:156)
      [STDERR] at javax.mail.Service.connect(Service.java:176)
      [STDERR] at com.myapp.Mailer.sendMsg(Mailer.java:45)
      [STDERR] at com.myapp.MailController.doPost(MailController.java:42)
      [STDERR] at com.myapp.MailController.doGet(MailController.java:26)
      


      Why am I getting this java.net.ConnectException: Connection refused exception?

      Happy programming,

      Mike