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 38 or later.
All you need to build this project is Java SE 17.0 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 a Greenmail mail server with all the required configuration. You will need to have installed a Container Engine capable of working 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 Greenmail 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. <<<<
Once you have finished with the Mail Quickstart, you can shutdown and remove the Greenmail 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. <<<<
Back Up the WildFly Standalone Server Configuration
Before you begin, back up your server configuration file.
-
If it is running, stop the WildFly server.
-
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
-
Open a terminal and navigate to the root of the WildFly directory.
-
Start the WildFly server with the default profile by typing the following command.
$ WILDFLY_HOME/bin/standalone.sh
NoteFor 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.
-
Before you begin, make sure you do the following:
-
Back up the WildFly standalone server configuration as described above.
-
Start the WildFly server with the standalone default profile as described above.
-
-
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 customMyOtherMail
mail session and configures it to use the custom outbound socket binding ports and default user credentials for SMTP and IMAP. -
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
NoteFor 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
-
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 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="{MailSubsystemNamespace}">
<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@mail.local" password="1234"/>
<pop3-server outbound-socket-binding-ref="my-pop3-binding"/>
<imap-server outbound-socket-binding-ref="my-imap-binding" username="user02@mail.local" password="1234"/>
</mail-session>
</subsystem>
Build and Deploy the Quickstart
-
Make sure WildFly server is started.
-
Open a terminal and navigate to the root directory of this quickstart.
-
Type the following command to build the quickstart.
$ mvn clean package
-
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.
:leveloffset: +1
|
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.
-
Make sure WildFly server is started.
-
Make sure the quickstart is deployed.
-
Type the following command to run the
verify
goal with theintegration-testing
profile activated.$ mvn verify -Pintegration-testing
Undeploy the Quickstart
When you are finished testing the quickstart, follow these steps to undeploy the archive.
-
Make sure WildFly server is started.
-
Open a terminal and navigate to the root directory of this quickstart.
-
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.
-
You can run the
remove-mail-session.cli
script provided in the root directory of this quickstart. -
You can manually restore the configuration using the backup copy of the configuration file.
Restore the WildFly Standalone Server Configuration by Running the JBoss CLI Script
-
Start the WildFly server as described above.
-
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
NoteFor 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.
-
If it is running, stop the WildFly server.
-
Replace the
WILDFLY_HOME/standalone/configuration/standalone.xml
file with the backup copy of the file.
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. The functionality is provided by the WildFly Maven Plugin, and you may find its configuration in the quickstart pom.xml
:
<profile>
<id>provisioned-server</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<discover-provisioning-info>
<version>${version.server}</version>
</discover-provisioning-info>
<add-ons>...</add-ons>
</configuration>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
</profile>
Note
|
When built, the provisioned WildFly server can be found in the |
Follow these steps to run the quickstart using the provisioned server.
-
Make sure the server is provisioned.
$ mvn clean package
-
Start the WildFly provisioned server, using the WildFly Maven Plugin
start
goal.$ mvn wildfly:start
-
Type the following command to run the integration tests.
$ mvn verify -Pintegration-testing
-
Shut down the WildFly provisioned server.
$ mvn wildfly:shutdown
Building and running the quickstart application with OpenShift
Build the WildFly Source-to-Image (S2I) Quickstart to OpenShift with Helm Charts
On OpenShift, the S2I build with Apache Maven uses an openshift
Maven profile to provision a WildFly server, deploy and run the quickstart in OpenShift environment.
The server provisioning functionality is provided by the WildFly Maven Plugin, and you may find its configuration in the quickstart pom.xml
:
<profile>
<id>openshift</id>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<discover-provisioning-info>
<version>${version.server}</version>
<context>cloud</context>
</discover-provisioning-info>
<add-ons>...</add-ons>
</configuration>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
</profile>
You may note that unlike the provisioned-server
profile it uses the cloud context which enables a configuration tuned for OpenShift environment.
Getting Started with WildFly for OpenShift and Helm Charts
This section contains the basic instructions to build and deploy this quickstart to WildFly for OpenShift using Helm Charts.
Prerequisites
-
You must be logged in OpenShift and have an
oc
client to connect to OpenShift -
Helm must be installed to deploy the backend on OpenShift.
Once you have installed Helm, you need to add the repository that provides Helm Charts for WildFly.
$ helm repo add wildfly https://docs.wildfly.org/wildfly-charts/
"wildfly" has been added to your repositories
$ helm search repo wildfly
NAME CHART VERSION APP VERSION DESCRIPTION
wildfly/wildfly ... ... Build and Deploy WildFly applications on OpenShift
wildfly/wildfly-common ... ... A library chart for WildFly-based applications
Install the Mail server on Openshift
The functionality of this quickstart depends on a running instance of the Greenmail Standalone server.
To deploy and configure the mail server, you will need to apply a set of configurations to your OpenShift cluster:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: greenmail
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: greenmail
template:
metadata:
labels:
app.kubernetes.io/name: greenmail
spec:
containers:
- name: greenmailserver
image: greenmail/standalone:2.0.1
---
apiVersion: v1
kind: Service
metadata:
name: greenmail
spec:
ports:
- name: "1465"
port: 1465
targetPort: 3465
- name: "1993"
port: 1993
targetPort: 3993
- name: "1025"
port: 1025
targetPort: 3025
- name: "1110"
port: 1110
targetPort: 3110
- name: "8081"
port: 8081
targetPort: 8080
- name: "1143"
port: 1143
targetPort: 3143
selector:
app.kubernetes.io/name: greenmail
type: ClusterIP
To make things simpler, you can find these commands in charts/greenmail-openshift.yaml
, and to apply them run the following command in your terminal:
$ oc apply -f charts/greenmail-openshift.yaml
Note
|
When done with the quickstart, the |
Deploy the WildFly Source-to-Image (S2I) Quickstart to OpenShift with Helm Charts
Log in to your OpenShift instance using the oc login
command.
The backend will be built and deployed on OpenShift with a Helm Chart for WildFly.
Navigate to the root directory of this quickstart and run the following command:
$ helm install mail -f charts/helm.yaml wildfly/wildfly --wait --timeout=10m0s
NAME: mail
...
STATUS: deployed
REVISION: 1
This command will return once the application has successfully deployed. In case of a timeout, you can check the status of the application with the following command in another terminal:
oc get deployment mail
The Helm Chart for this quickstart contains all the information to build an image from the source code using S2I on Java 17:
build:
uri: https://github.com/wildfly/quickstart.git
ref: main
contextDir: mail
deploy:
replicas: 1
env:
- name: MAIL_SERVER_ADDRESS
value: greenmail
This will create a new deployment on OpenShift and deploy the application.
If you want to see all the configuration elements to customize your deployment you can use the following command:
$ helm show readme wildfly/wildfly
Get the URL of the route to the deployment.
$ oc get route mail -o jsonpath="{.spec.host}"
Access the application in your web browser using the displayed URL.
Run the Integration Tests with OpenShift
The integration tests included with this quickstart, which verify that the quickstart runs correctly, may also be run with the quickstart running on OpenShift.
Note
|
The integration tests expect a deployed application, so make sure you have deployed the quickstart on OpenShift before you begin. |
Run the integration tests using the following command to run the verify
goal with the integration-testing
profile activated and the proper URL:
$ mvn verify -Pintegration-testing -Dserver.host=https://$(oc get route mail --template='{{ .spec.host }}')
Note
|
The tests are using SSL to connect to the quickstart running on OpenShift. So you need the certificates to be trusted by the machine the tests are run from. |
Undeploy the WildFly Source-to-Image (S2I) Quickstart from OpenShift with Helm Charts
$ helm uninstall mail
Building and running the quickstart application with Kubernetes
Build the WildFly Quickstart to Kubernetes with Helm Charts
For Kubernetes, the build with Apache Maven uses an openshift
Maven profile to provision a WildFly server, suitable for running on Kubernetes.
The server provisioning functionality is provided by the WildFly Maven Plugin, and you may find its configuration in the quickstart pom.xml
:
<profile>
<id>openshift</id>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<discover-provisioning-info>
<version>${version.server}</version>
<context>cloud</context>
</discover-provisioning-info>
<add-ons>...</add-ons>
</configuration>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
</profile>
You may note that unlike the provisioned-server
profile it uses the cloud context which enables a configuration tuned for Kubernetes environment.
Getting Started with Kubernetes and Helm Charts
This section contains the basic instructions to build and deploy this quickstart to Kubernetes using Helm Charts.
Install Kubernetes
In this example we are using Minikube as our Kubernetes provider. See the Minikube Getting Started guide for how to install it. After installing it, we start it with 4GB of memory.
minikube start --memory='4gb'
The above command should work if you have Docker installed on your machine. If, you are using Podman instead of Docker, you will also need to pass in --driver=podman
, as covered in the Minikube documentation.
Once Minikube has started, we need to enable its registry since that is where we will push the image needed to deploy the quickstart, and where we will tell the Helm charts to download it from.
minikube addons enable registry
In order to be able to push images to the registry we need to make it accessible from outside Kubernetes. How we do this depends on your operating system. All the below examples will expose it at localhost:5000
# On Mac:
docker run --rm -it --network=host alpine ash -c "apk add socat && socat TCP-LISTEN:5000,reuseaddr,fork TCP:$(minikube ip):5000"
# On Linux:
kubectl port-forward --namespace kube-system service/registry 5000:80 &
# On Windows:
kubectl port-forward --namespace kube-system service/registry 5000:80
docker run --rm -it --network=host alpine ash -c "apk add socat && socat TCP-LISTEN:5000,reuseaddr,fork TCP:host.docker.internal:5000"
Prerequisites
-
Helm must be installed to deploy the backend on Kubernetes.
Once you have installed Helm, you need to add the repository that provides Helm Charts for WildFly.
$ helm repo add wildfly https://docs.wildfly.org/wildfly-charts/
"wildfly" has been added to your repositories
$ helm search repo wildfly
NAME CHART VERSION APP VERSION DESCRIPTION
wildfly/wildfly ... ... Build and Deploy WildFly applications on OpenShift
wildfly/wildfly-common ... ... A library chart for WildFly-based applications
Install the Mail server on Kubernetes
The functionality of this quickstart depends on a running instance of the Greenmail Standalone server.
To deploy and configure the mail server, you will need to apply a set of configurations to your Kubernetes cluster:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: greenmail
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: greenmail
template:
metadata:
labels:
app.kubernetes.io/name: greenmail
spec:
containers:
- name: greenmailserver
image: greenmail/standalone:2.0.1
---
apiVersion: v1
kind: Service
metadata:
name: greenmail
spec:
ports:
- name: "1465"
port: 1465
targetPort: 3465
- name: "1993"
port: 1993
targetPort: 3993
- name: "1025"
port: 1025
targetPort: 3025
- name: "1110"
port: 1110
targetPort: 3110
- name: "8081"
port: 8081
targetPort: 8080
- name: "1143"
port: 1143
targetPort: 3143
selector:
app.kubernetes.io/name: greenmail
type: ClusterIP
To make things simpler, you can find these commands in charts/greenmail-kubernetes.yaml
, and to apply them run the following command in your terminal:
$ kubectl apply -f charts/greenmail-kubernetes.yaml --wait --timeout=10m0s
Note
|
When done with the quickstart, the |
Deploy the WildFly Source-to-Image (S2I) Quickstart to Kubernetes with Helm Charts
The backend will be built and deployed on Kubernetes with a Helm Chart for WildFly.
Navigate to the root directory of this quickstart and run the following commands:
mvn -Popenshift package wildfly:image
This will use the openshift
Maven profile we saw earlier to build the application, and create a Docker image containing the WildFly server with the application deployed. The name of the image will be mail
.
Next we need to tag the image and make it available to Kubernetes. You can push it to a registry like quay.io
. In this case we tag as localhost:5000/mail:latest
and push it to the internal registry in our Kubernetes instance:
# Tag the image
docker tag mail localhost:5000/mail:latest
# Push the image to the registry
docker push localhost:5000/mail:latest
In the below call to helm install
which deploys our application to Kubernetes, we are passing in some extra arguments to tweak the Helm build:
-
--set build.enabled=false
- This turns off the s2i build for the Helm chart since Kubernetes, unlike OpenShift, does not have s2i. Instead, we are providing the image to use. -
--set deploy.route.enabled=false
- This disables route creation normally performed by the Helm chart. On Kubernetes we will use port-forwards instead to access our application, since routes are an OpenShift specific concept and thus not available on Kubernetes. -
--set image.name="localhost:5000/mail"
- This tells the Helm chart to use the image we built, tagged and pushed to Kubernetes' internal registry above.
$ helm install mail -f charts/helm.yaml wildfly/wildfly --wait --timeout=10m0s --set build.enabled=false --set deploy.route.enabled=false --set image.name="localhost:5000/mail"
NAME: mail
...
STATUS: deployed
REVISION: 1
This command will return once the application has successfully deployed. In case of a timeout, you can check the status of the application with the following command in another terminal:
kubectl get deployment mail
The Helm Chart for this quickstart contains all the information to build an image from the source code using S2I on Java 17:
build:
uri: https://github.com/wildfly/quickstart.git
ref: main
contextDir: mail
deploy:
replicas: 1
env:
- name: MAIL_SERVER_ADDRESS
value: greenmail
This will create a new deployment on Kubernetes and deploy the application.
If you want to see all the configuration elements to customize your deployment you can use the following command:
$ helm show readme wildfly/wildfly
To be able to connect to our application running in Kubernetes from outside, we need to set up a port-forward to the mail
service created for us by the Helm chart.
This service will run on port 8080
, and we set up the port forward to also run on port 8080
:
kubectl port-forward service/mail 8080:8080
The server can now be accessed via http://localhost:8080
from outside Kubernetes. Note that the command to create the port-forward will not return, so it is easiest to run this in a separate terminal.
Run the Integration Tests with Kubernetes
The integration tests included with this quickstart, which verify that the quickstart runs correctly, may also be run with the quickstart running on Kubernetes.
Note
|
The integration tests expect a deployed application, so make sure you have deployed the quickstart on Kubernetes before you begin. |
Run the integration tests using the following command to run the verify
goal with the integration-testing
profile activated and the proper URL:
$ mvn verify -Pintegration-testing -Dserver.host=http://localhost:8080
Undeploy the WildFly Source-to-Image (S2I) Quickstart from Kubernetes with Helm Charts
$ helm uninstall mail
To stop the port forward you created earlier use:
$ kubectl port-forward service/mail 8080:8080