4 Replies Latest reply on Jan 28, 2009 8:53 AM by Ankit Garg

    Problem with sending Email from jBoss

    Ankit Garg Newbie

      I want to know how to send mails from jBoss. A google search revealed something which confused me. It said that mail sending is done through a bean java:/email or something. This confused me. I am using javax.mail.Transport to send the mail. The code is like this

      Properties props = new Properties();
      Session mailConnection = Session.getInstance(props, null);
      Transport tr = mailConnection.getTransport("smtp");

      Now it gives an exception when when the connect method is called as-

      javax.mail.MessagingException: Could not connect to SMTP host:, port: 25;
      nested exception is:
      java.net.SocketException: Software caused connection abort: connect

      Now there are a few things I am not sure of. First of all I think the username and password given when calling connect method are those defined in the mail-service.xml in jBoss. Also is mailing on directly in jBoss or do I have to start it by configuring it.

      I would be thankful to anyone who could help...

      (I have also posted this on coderanch.com but not getting any reply there so cross posted it here)

        • 1. Re: Problem with sending Email from jBoss
          Andre Ehrlich Newbie


          In one of my lasts projects I wrote an MDB, which reads text messages from a queue and sends mails to some my mail account. Maybe this helps you a little.

          First of all, you have to set up the JBoss mail service in server//deploy/mail-service.xml. You need to specify the connection properties to the mail server which is used.

          Secondly you write the code to send a mail, for example:

          // who should receive the mail
          Address[] to = InternetAddress.parse("duke@java.sun.com,homer@simpson.net", false);
          // create a simple text message
          javax.mail.Message message = new MimeMessage(session);
          message.setRecipients(javax.mail.Message.RecipientType.TO, to);
          message.setSubject("Mail subject goes here");
          message.setSentDate(new Date());
          message.setText("java mail rocks!");
          // ... and send it

          That's it it :-) If you do not specify the "from" property, the default value defined in mail-service.xml is used.

          Hope that helped! Keep asking, if you have any problems.


          • 2. Re: Problem with sending Email from jBoss
            Ankit Garg Newbie

            j-n00b thanks for the help. But the main problem is still there. How to configure the mail-service.xml file. As far as I read on internet, if I use jBoss mail service, then I have to acquire the mail session using a JNDI lookup of java:Email right?? Also I have to provide a provider in the mail-service.xml. Now I don't know which provider I can use. My localhost provider seems not to work. So I need an external provider. Can you tell me any free providers from which I can test my code??

            • 3. Re: Problem with sending Email from jBoss
              Andre Ehrlich Newbie

              Sorry, I forgot to send the snippet where the mail session is injected. So let's do it step by step.

              As provider you could use any mail server, you have access to (including GMX, gmail or whatever). If you don't have one available, you can install your own. I use JES (Java Email Server) for that purpose. It is a real simple and easy to use server (only 2 config files!), which is quite perfect for testing issues.

              Assuming you have JES running, your mail-service.xml could look like this:

              <?xml version="1.0" encoding="UTF-8"?>
               <mbean code="org.jboss.mail.MailService"
               <attribute name="JNDIName">mail/MailSession</attribute>
               <!-- mail server login data, not required for JES -->
               <attribute name="User">homer</attribute>
               <attribute name="Password">simpson</attribute>
               <attribute name="Configuration">
               <property name="mail.store.protocol" value="pop3"/>
               <property name="mail.transport.protocol" value="smtp"/>
               <!-- who receives the mail, if no recipient is specified -->
               <property name="mail.user" value="nobody"/>
               <!-- Change to the mail server -->
               <property name="mail.pop3.host" value="localhost"/>
               <!-- Change to the SMTP gateway server -->
               <property name="mail.smtp.host" value="localhost"/>
               <!-- The mail server port -->
               <property name="mail.smtp.port" value="25"/>
               <!-- who is the sender of the mail, if none is specified -->
               <property name="mail.from" value="mailmaster@asdf.de"/>
               <!-- Enable debugging output from the javamail classes -->
               <property name="mail.debug" value="false"/>

              At least, this config works for me :-)

              Back in your application, you can use the earlier code snippet in an EJB to send a mail. There is no need to look up the mail session or something, this is done by JBoss via Dependency Injection. Here is a simple example:
              package ae;
              import java.net.InetAddress;
              import java.util.Date;
              import javax.annotation.Resource;
              import javax.ejb.ActivationConfigProperty;
              import javax.ejb.MessageDriven;
              import javax.ejb.MessageDrivenContext;
              import javax.jms.JMSException;
              import javax.jms.Message;
              import javax.jms.MessageListener;
              import javax.jms.TextMessage;
              import javax.mail.Address;
              import javax.mail.Session;
              import javax.mail.Transport;
              import javax.mail.internet.InternetAddress;
              import javax.mail.internet.MimeMessage;
              import org.apache.log4j.Logger;
              @MessageDriven(activationConfig = {
               @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
               @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/q2m") })
              public class Queue2MailBean implements MessageListener {
               private static final Logger log = Logger.getLogger(Queue2MailBean.class);
               @Resource(mappedName = "mail/MailSession")
               private Session session;
               public void onMessage(Message inMessage) {
               try {
               // text message
               if (inMessage instanceof TextMessage) {
               log.info("processing incomming TextMessage");
               TextMessage textMsg = (TextMessage) inMessage;
               int number = textMsg.getIntProperty("asdf");
               sendMail("andre@asdf.de", "Received number " + number);
               // other message types are currently not supported!
               else {
               log.warn("Message of wrong type: " + inMessage.getClass().getName());
               } catch (JMSException e) {
               log.error("Q2M: Error while receiving JMS message!", e);
               private void sendMail(String recipient, String text) {
               try {
               Address[] to = InternetAddress.parse(recipient, false);
               // create message
               javax.mail.Message message = new MimeMessage(session);
               message.setRecipients(javax.mail.Message.RecipientType.TO, to);
               message.setSentDate(new Date());
               // Send message
               } catch (Exception e) {
               log.error(e, e);

              My Bean reads TextMessages from a Queue named "q2m" (must be deployed manually or via a "-service.xml" contained in the ejb-jar. Within the text messages, there is an int property stored under key "asdf", which is forwarded to my JES mail address. The sendMail() method uses the MailSession injected by JBoss (see @Resource) to send the mail using the default FROM attribute.

              If you don't like to use Dependency Injection, you can lookup the mail session yourself:
              session = (Session) new InitialContext().lookup("Mail");

              Please note, that this does not work outside of the server VM, because the mail session is bound to the "java" namespace, see serverlog: 2009-01-22 14:21:38,187 INFO [org.jboss.mail.MailService] (main) Mail Service bound to java:/Mail.

              Are there any questions left?


              • 4. Re: Problem with sending Email from jBoss
                Ankit Garg Newbie

                Thanks j-n00b for the wonderful explanation. I'll try this and tell you if I get any problems.

                Thanks again :)