Conifgure JBoss mail-service for SMTP over SSL


I always have troubles with sending email-messages from my application which I host locally.
The issue is that my ISP provider SMTP works only via SSL. I was able to setup Outlook to work with it and since ISP use not trusted certificate I was asked to confirm that that certificate is trusted.
So, actually (going ahead) we have two problems

  1. Configure /deploy/mail-service.xml to use correct port and other SSL parameters
  2. Add ISP certificate into the java keystore (since the certificate is not trusted)

So, after couple of experiments and googling I find that following combination of parameters work for me

<server>
    <mbean code="org.jboss.mail.MailService" name="jboss:service=Mail">
        <attribute name="JNDIName">java:/Mail</attribute>
        <attribute name="User">${username}</attribute>
        <attribute name="Password">${password}</attribute>

        <attribute name="Configuration">
            <configuration>
                <!-- Change to your mail server prototocol -->
                <property name="mail.store.protocol" value="pop3"></property>
                <property name="mail.transport.protocol" value="smtp"></property>

                <!-- Change to the user who will receive mail  -->
                <property name="mail.user" value="${username}"></property>

                <!-- Change to the mail server  -->
                <property name="mail.pop3.host" value="${pop3-server}"></property>

                <!-- SSL parameters STARTED-->
                <property name="mail.smtp.host" value="${smtp-server}"></property>
                <property name="mail.smtp.auth" value="true"></property>
                <!-- The mail server port -->
                <property name="mail.smtp.port" value="465"></property>
                <property name="mail.smtp.ssl.enable" value="true"></property>
                <property name="mail.smtp.socketFactory.class" value="javax.net.ssl.SSLSocketFactory"></property>
                <!-- SSL parameters ENDED -->

                <property name="mail.smtp.connectiontimeout" value="20000"></property>
                <property name="mail.smtp.timeout" value="20000"></property>

                <!-- Change to the address mail will be from  -->
                <property name="mail.from" value="${yourname@yourisp}"></property>

                <!-- Enable debugging to see a lot of inetresting about SMTP protocol -->
                <property name="mail.debug" value="true"></property>
            </configuration>
        </attribute>
        <depends>jboss:service=Naming</depends>
    </mbean>
</server>

So, that configuration seems at least trying to connect to my SMTP server, but emails are still not sent out. The reason is the SSL-certificate which my ISP used is signed by cacert.org, which issue free certificates.
To fix this issue I have to install that certificate into my java keystore and the easiest way to do it was to use InstallCert program (the link to source-code)
I run java my-smtp-server:465 input “1” and program generate jssecacerts. After that I updated cacerts file in my %JRE-HOME%/lib/security with generated file (of course I backed up previous cacerts) and that was enough to start using my SSL email.

To find more details or solve more issues you can read the JavaMail FAQ which cover a lot of topics, for example I think it worth to use gmail or yahoo smtp for your email testing purposes, since they are always present and you are not depend on your ISP SMTP

About these ads

https://www.facebook.com/achorniy

Tagged with: , , , , , ,
Posted in Software Development, Tips and Tricks
4 comments on “Conifgure JBoss mail-service for SMTP over SSL
  1. pokuri says:

    Can you please provide complete example of using mail service in JBOSS. I just want to implement that into my application where I use jBOSS.

    • Andrey Chorniy says:

      Hi, I use it from Seam application and it’s pretty easy (you may find complete instructions here)
      It’s pretty easy with Seam
      1. Configure application (inject JNDI mailService)

      <components xmlns="http://jboss.com/products/seam/components"
          xmlns:core="http://jboss.com/products/seam/core"
          xmlns:mail="http://jboss.com/products/seam/mail">
      
          <mail:mail-session session-jndi-name="java:/Mail"/>
      </components>
      

      2. Define message template

      <m:message xmlns="http://www.w3.org/1999/xhtml"
          xmlns:m="http://jboss.com/products/seam/mail"
          xmlns:h="http://java.sun.com/jsf/html">
      
          <m:from name="Andrey" address="Andrey@example.com" />
          <m:to name="#{person.firstname} #{person.lastname}">#{person.address}</m:to>
          <m:subject>Try out Seam!</m:subject>
          <m:body>
              <p><h:outputText value="Dear #{person.firstname}" />,</p>
              <p>Regards,</p>
              <p>Andrey</p>
          </m:body>   
      </m:message>
      

      3. Send from Seam action (java-code)

      @In(create=true)
      private Renderer renderer;
      public void send() {
          try {
             renderer.render("/simple.xhtml");
             facesMessages.add("Email sent successfully");
         }  catch (Exception e) {
             facesMessages.add("Email sending failed: " + e.getMessage());
         }
      }
      

      too easy to be true, but it’s true :-)

      If you don’t use seam – you probably have to use Java-Mail API
      For Spring application you could use Spring Mail API and JNDI bound mail-session

      <bean id="mailSender"
              class="org.springframework.mail.javamail.JavaMailSenderImpl">
              <property name="session"><ref bean="mailSession"></property>
      </bean>
      
      <bean id="mailSession"
              class="org.springframework.jndi.JndiObjectFactoryBean">
              <property name="jndiName"><value>java:/Mail</value></property>
      </bean>
      

      If you don’t have Spring and don’t have Seam than you probably have to retrieve and use your JNDI bound mail-sesison with following java-code (see full example in the jboss-docs

      Context initCtx = new InitialContext();
      Context envCtx = (Context) initCtx.lookup("java:comp/env");
      Session session = (Session) envCtx.lookup("mail/Session");
      
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress(request.getParameter("from"));
      InternetAddress to[] = new InternetAddress[1];
      to[0] = new InternetAddress(request.getParameter("to"));
      message.setRecipients(Message.RecipientType.TO, to);
      message.setSubject(request.getParameter("subject"));
      message.setContent(request.getParameter("content"), "text/plain");
      Transport.send(message);
      
  2. Warren Socks says:

    While I think this is extremely good information I’m not sure if it is for me

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 32 other followers

%d bloggers like this: