The mail quickstart demonstrates how to send and receive emails using CDI and JSF and with custom Mail provider configured in WildFly.

What is it?

The mail quickstart demonstrates sending and receiving emails with the use of CDI (Contexts and Dependency Injection) and JSF (JavaServer Faces) in WildFly Application Server.

The mail provider is configured in the mail subsystem of the WILDFLY_HOME/standalone/configuration/standalone.xml configuration file if you are running a standalone server or in the WILDFLY_HOME/domain/configuration/domain.xml configuration file if you are running in a managed domain.

You can use the default mail provider that comes out of the box with WildFly. It uses your local mail relay and the default SMTP port of 25. However, this quickstart demonstrates how to define and use a custom mail provider.

This example is a web application that takes To, From, Subject, and Message Body input and sends mail using SMTP. These emails can be later read by using IMAP or POP3. The front end is a JSF page with a simple POJO backing, leveraging CDI for resource injection.

System Requirements

The application this project produces is designed to be run on WildFly Application Server 32 or later.

All you need to build this project is Java 11.0 (Java SDK 11) or later and Maven 3.6.0 or later. See Configure Maven to Build and Deploy the Quickstarts to make sure you are configured correctly for testing the quickstarts.

Use of the WILDFLY_HOME and QUICKSTART_HOME Variables

In the following instructions, replace WILDFLY_HOME with the actual path to your WildFly installation. The installation path is described in detail here: Use of WILDFLY_HOME and JBOSS_HOME Variables.

When you see the replaceable variable QUICKSTART_HOME, replace it with the path to the root directory of all of the quickstarts.

Configure a Mail Server on Your Local Machine

To run the Mail Quickstart, you need a Mail Server configured with the following protocols and ports:

  • SMTP port:1025

  • POP3 port:1110

  • IMAP port:1143

In addition, the Mail Subsystem configuration and the test cases expect you have the following Mail accounts configured on your Mail Server:

You can use any Mail Server you consider, although to facilitate this task, you will find under the Mail Quickstart root directory a docker compose file prepared to launch an Apache James Mail server with all the required configuration. You will need to have installed a Container Engine capable of work with Docker compose files and Linux images. The following command assumes you have Podman and Podman Compose installed in your local environment.

To launch the Apache James Mail server, open the terminal and navigate to the Mail Quickstart root directory and execute the following:

$ podman compose up --wait
>>>> Executing external compose provider "/usr/local/bin/docker-compose". Please refer to the documentation for details. <<<<

[+] Running 1/1
 ✔ Container apache-james  Healthy
Note
The Apache James server is configured without allowing the relay of the emails to external addresses that are not configured in the server. When you are sending / receiving emails with this server, you have to use the accounts shipped with the apache James demo image. These are the accounts available out of the box: user01@james.local, user02@james.local and user03@james.local. All accounts use the same password: 1234

Once you have finished with the Mail Quickstart, you can shutdown and remove the Apache James Mail server with the following command:

$ podman compose down --volumes
>>>> Executing external compose provider "/usr/local/bin/docker-compose". Please refer to the documentation for details. <<<<

[+] Running 2/1
 ✔ Container apache-james  Removed
 ✔ Network mail_default    Removed

Back Up the WildFly Standalone Server Configuration

Before you begin, back up your server configuration file.

  1. If it is running, stop the WildFly server.

  2. Back up the WILDFLY_HOME/standalone/configuration/standalone.xml file.

After you have completed testing this quickstart, you can replace this file to restore the server to its original configuration.

Start the WildFly Standalone Server

  1. Open a terminal and navigate to the root of the WildFly directory.

  2. Start the WildFly server with the default profile by typing the following command.

    $ WILDFLY_HOME/bin/standalone.sh 
    Note
    For Windows, use the WILDFLY_HOME\bin\standalone.bat script.

Configure the Server

You configure the custom mail session in WildFly by running Management CLI commands. For your convenience, this quickstart batches the commands into a configure-mail-session.cli script provided in the root directory of this quickstart.

  1. Before you begin, make sure you do the following:

  2. Review the configure-mail-session.cli file in the root of this quickstart directory. This script creates custom outbound socket binding port for SMTP, POP3, and IMAP. It then creates the custom MyOtherMail mail session and configures it to use the custom outbound socket binding ports and default user credentials for SMTP and IMAP.

  3. Open a new terminal, navigate to the root directory of this quickstart, and run the following command, replacing WILDFLY_HOME with the path to your server:

    $ WILDFLY_HOME/bin/jboss-cli.sh --connect --file=configure-mail-session.cli
    Note
    For Windows, use the WILDFLY_HOME\bin\jboss-cli.bat script.

    You should see the following result when you run the script.

    The batch executed successfully
    process-state: reload-required
  4. Stop the WildFly server.

Review the Modified Server Configuration

After stopping the server, open the WILDFLY_HOME/standalone/configuration/standalone.xml file and review the changes.

The following outbound-socket-binding groups are added to the standard-sockets <socket-binding-group> element.

<socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:0}">
  ...
  </outbound-socket-binding>
  <outbound-socket-binding name="my-imap-binding">
      <remote-destination host="localhost" port="1143"/>
  </outbound-socket-binding>
  <outbound-socket-binding name="my-pop3-binding">
      <remote-destination host="localhost" port="1110"/>
  </outbound-socket-binding>
  <outbound-socket-binding name="my-smtp-binding">
     <remote-destination host="localhost" port="1025"/>
  </outbound-socket-binding>
</socket-binding-group>

The MyOtherMail mail session is added to the mail subsystem and configured to use the custom outbound socket binding ports.

<subsystem xmlns="urn:jboss:domain:mail:3.0">
   <mail-session name="default" jndi-name="java:jboss/mail/Default">
      <smtp-server outbound-socket-binding-ref="mail-smtp"/>
   </mail-session>
   <mail-session name="MyOtherMail" debug="true" jndi-name="java:jboss/mail/MyOtherMail">
     <smtp-server outbound-socket-binding-ref="my-smtp-binding" username="user01@james.local" password="1234"/>
     <pop3-server outbound-socket-binding-ref="my-pop3-binding"/>
     <imap-server outbound-socket-binding-ref="my-imap-binding" username="user02@james.local" password="1234"/>
   </mail-session>
</subsystem>

Build and Deploy the Quickstart

  1. Make sure WildFly server is started.

  2. Open a terminal and navigate to the root directory of this quickstart.

  3. Type the following command to build the quickstart.

    $ mvn clean package
  4. Type the following command to deploy the quickstart.

    $ mvn wildfly:deploy

This deploys the mail/target/mail.war to the running instance of the server.

You should see a message in the server log indicating that the archive deployed successfully.

Access the Application

The application will be running at the following URL: http://localhost:8080/mail/.

Note
If you see Error processing request in the browser when you access the application and attempt to send email, followed by jakarta.servlet.ServletException: MailConnectException: Couldn't connect to host, port: localhost, 1025; timeout -1; nested exception is: java.net.ConnectException: Connection refused, make sure you followed the instructions above to Configure an SMTP Server on Your Local Machine.
Note
If you are using the Mail server shipped with this Quickstart and see Error sending the Email. Invalid Addresses in the browser when you attempt to send email, make sure you are sending your email to an existing account configured in the Mail Server since by default Apache James demo image is shipped with relay disabled. By default, Apache James demo image has the following accounts configured: user01@james.local, user02@james.local and user03@james.local.

Run the Integration Tests

This quickstart includes integration tests, which are located under the src/test/ directory. The integration tests verify that the quickstart runs correctly when deployed on the server.

Follow these steps to run the integration tests.

  1. Make sure WildFly server is started.

  2. Make sure the quickstart is deployed.

  3. Type the following command to run the verify goal with the integration-testing profile activated.

    $ mvn verify -Pintegration-testing 

Undeploy the Quickstart

When you are finished testing the quickstart, follow these steps to undeploy the archive.

  1. Make sure WildFly server is started.

  2. Open a terminal and navigate to the root directory of this quickstart.

  3. Type this command to undeploy the archive:

    $ mvn wildfly:undeploy

Restore the WildFly Standalone Server Configuration

You can restore the original server configuration using either of the following methods.

Restore the WildFly Standalone Server Configuration by Running the JBoss CLI Script

  1. Start the WildFly server as described above.

  2. Open a new terminal, navigate to the root directory of this quickstart, and run the following command, replacing WILDFLY_HOME with the path to your server:

    $ WILDFLY_HOME/bin/jboss-cli.sh --connect --file=remove-mail-session.cli
    Note
    For Windows, use the WILDFLY_HOME\bin\jboss-cli.bat script.

This script removes the custom MyOtherMail session from the mail subsystem in the server configuration. file You should see the following result when you run the script:

The batch executed successfully
process-state: reload-required

Restore the WildFly Standalone Server Configuration Manually

When you have completed testing the quickstart, you can restore the original server configuration by manually restoring the backup copy the configuration file.

  1. If it is running, stop the WildFly server.

  2. Replace the WILDFLY_HOME/standalone/configuration/standalone.xml file with the backup copy of the file.

Run the Quickstart in Red Hat CodeReady Studio or Eclipse

You can also start the server and deploy the quickstarts or run the Arquillian tests in Red Hat CodeReady Studio or from Eclipse using JBoss tools. For general information about how to import a quickstart, add a WildFly server, and build and deploy a quickstart, see Use Red Hat CodeReady Studio or Eclipse to Run the Quickstarts.

  • Make sure you Configure an SMTP Server on Your Local Machine.

  • Make sure you configure the WildFly custom mail configuration as described above under Configure the WildFly Server. Stop the server at the end of that step.

  • To deploy the server project, right-click on the mail project and choose Run As –> Run on Server. A browser window appears that accesses the running application.

  • To undeploy the project, right-click on the mail project and choose Run As –> Maven build. Enter wildfly:undeploy for the Goals and click Run.

  • Make sure you restore the WildFly server configuration when you have completed testing this quickstart.

Debug the Application

If you want to debug the source code of any library in the project, run the following command to pull the source into your local repository. The IDE should then detect it.

$ mvn dependency:sources

Building and running the quickstart application with provisioned WildFly server

Instead of using a standard WildFly server distribution, you can alternatively provision a WildFly server to deploy and run the quickstart, by activating the Maven profile named provisioned-server when building the quickstart:

$ mvn clean package -Pprovisioned-server

The provisioned WildFly server, with the quickstart deployed, can then be found in the target/server directory, and its usage is similar to a standard server distribution, with the simplification that there is never the need to specify the server configuration to be started.

The server provisioning functionality is provided by the WildFly Maven Plugin, and you may find its configuration in the quickstart pom.xml:

        <profile>
            <id>provisioned-server</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.wildfly.plugins</groupId>
                        <artifactId>wildfly-maven-plugin</artifactId>
                        <configuration>
                            <feature-packs>
                                <feature-pack>
                                    <location>org.wildfly:wildfly-galleon-pack:${version.server}</location>
                                </feature-pack>
                            </feature-packs>
                            <layers>...</layers>
                            <!-- deploys the quickstart on root web context -->
                            <name>ROOT.war</name>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>package</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    ...
                </plugins>
            </build>
        </profile>
Note

Since the plugin configuration above deploys quickstart on root web context of the provisioned server, the URL to access the application should not have the /mail path segment after HOST:PORT.

Run the Integration Tests with a provisioned server

The integration tests included with this quickstart, which verify that the quickstart runs correctly, may also be run with a provisioned server.

Follow these steps to run the integration tests.

  1. Make sure the server is provisioned.

    $ mvn clean package -Pprovisioned-server
  2. Start the WildFly provisioned server, this time using the WildFly Maven Plugin, which is recommended for testing due to simpler automation. The path to the provisioned server should be specified using the jbossHome system property.

    $ mvn wildfly:start -DjbossHome=target/server 
  3. Type the following command to run the verify goal with the integration-testing profile activated, and specifying the quickstart’s URL using the server.host system property, which for a provisioned server by default is http://localhost:8080.

    $ mvn verify -Pintegration-testing -Dserver.host=http://localhost:8080 
  4. Shutdown the WildFly provisioned server, this time using the WildFly Maven Plugin too.

    $ mvn wildfly:shutdown