include::../shared-doc/attributes.adoc[]

= mail: E-Mail Example using CDI and JSF
:author: Joel Tosi
:level: Beginner
:technologies: JavaMail, CDI, JSF

[abstract]
The `mail` quickstart demonstrates how to send email using CDI and JSF and the default Mail provider that ships with {productName}.

:standalone-server-type: default
:archiveType: war
:includes-cli-scripts:
:restoreScriptName: remove-mail-session.cli

== What is it?

The `mail` quickstart demonstrates sending email with the use of _CDI_ (Contexts and Dependency Injection) and _JSF_ (JavaServer Faces) in {productNameFull}.

The mail provider is configured in the `mail` subsystem of the `__{jbossHomeName}__/standalone/configuration/standalone.xml` configuration file if you are running a standalone server or in the `__{jbossHomeName}__/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 {productName}. 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 to that address. The front end is a JSF page with a simple POJO backing, leveraging CDI for resource injection.

// System Requirements
include::../shared-doc/system-requirements.adoc[leveloffset=+1]
// Use of {jbossHomeName}
include::../shared-doc/use-of-jboss-home-name.adoc[leveloffset=+1]

[[configure_an_smtp_server_on_your_local_machine]]
== Configure an SMTP Server on Your Local Machine

This quickstart expects that you have an SMTP mail server running on your machine and configured for the default port `localhost:25`.
To configure an SMTP mail server, consult the documentation for your operating system. It is beyond the scope of this quickstart to provide these instructions.

If you do not configure an SMTP mail server on your local machine, you will see the exception `com.sun.mail.util.MailConnectException:  Couldn&#39;t connect to host, port: localhost, 25; timeout -1;` when you access the application and attempt to send an email.

// Back Up the {productName} Standalone Server Configuration
include::../shared-doc/back-up-server-standalone-configuration.adoc[leveloffset=+1]
// Start the {productName} Standalone Server
include::../shared-doc/start-the-standalone-server.adoc[leveloffset=+1]

[[configure_the_server]]
== Configure the Server

You configure the custom mail session in {productName} 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:

* xref:back_up_standalone_server_configuration[Back up the {productName} standalone server configuration] as described above.
* xref:start_the_eap_standalone_server[Start the {productName} 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 custom `MyOtherMail` mail session and configures it to use the custom outbound socket binding ports.
. Open a new terminal, navigate to the root directory of this quickstart, and run the following command, replacing `__{jbossHomeName}__` with the path to your server:
+
[source,subs="+quotes,attributes+",options="nowrap"]
----
$ __{jbossHomeName}__/bin/jboss-cli.sh --connect --file=configure-mail-session.cli
----
+
NOTE: For Windows, use the `__{jbossHomeName}__\bin\jboss-cli.bat` script.

+
You should see the following result when you run the script.
+
[source,options="nowrap"]
----
The batch executed successfully
process-state: reload-required
----

. Stop the {productName} server.

== Review the Modified Server Configuration

After stopping the server, open the `__{jbossHomeName}__/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.

[source,xml,options="nowrap"]
----
<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-smtp-binding">
      <remote-destination host="localhost" port="25"/>
  </outbound-socket-binding>
  <outbound-socket-binding name="my-pop3-binding">
      <remote-destination host="localhost" port="110"/>
  </outbound-socket-binding>
  <outbound-socket-binding name="my-imap-binding">
      <remote-destination host="localhost" port="143"/>
  </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.

[source,xml,subs="attributes+",options="nowrap"]
----
<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" jndi-name="java:jboss/mail/MyOtherMail">
      <smtp-server password="pass" username="nobody" tls="true" outbound-socket-binding-ref="my-smtp-binding"/>
      <pop3-server outbound-socket-binding-ref="my-pop3-binding"/>
      <imap-server password="pass" username="nobody" outbound-socket-binding-ref="my-imap-binding"/>
   </mail-session>
</subsystem>
----

// Build and Deploy the Quickstart
include::../shared-doc/build-and-deploy-the-quickstart.adoc[leveloffset=+1]

== Access the Application

The application will be running at the following URL: http://localhost:8080/{artifactId}/.

NOTE: If you see `Error processing request` in the browser when you access the application and attempt to send email, followed by `javax.servlet.ServletException: com.sun.mail.util.MailConnectException: Couldn&#39;t connect to host, port: localhost, 25; timeout -1; nested exception is: java.net.ConnectException: Connction refused`, make sure you followed the instructions above to xref:configure_an_smtp_server_on_your_local_machine[Configure an SMTP Server on Your Local Machine].

// Undeploy the Quickstart
include::../shared-doc/undeploy-the-quickstart.adoc[leveloffset=+1]
// Restore the {productName} Standalone Server Configuration
include::../shared-doc/restore-standalone-server-configuration.adoc[leveloffset=+1]

// Additional information about this 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:

[source,options="nowrap"]
----
The batch executed successfully
process-state: reload-required
----

// Restore the {productName} Standalone Server Configuration Manually
include::../shared-doc/restore-standalone-server-configuration-manual.adoc[leveloffset=+2]
// Run the Quickstart in Red Hat JBoss Developer Studio or Eclipse
include::../shared-doc/run-the-quickstart-in-jboss-developer-studio.adoc[leveloffset=+1]

// Additional JBoss Developer Studio instructions
* Make sure you xref:configure_an_smtp_server_on_your_local_machine[Configure an SMTP Server on Your Local Machine].
* Make sure you configure the {productName} custom mail configuration as described above under xref:configure_the_server[Configure the {productName} Server]. Stop the server at the end of that step.
* To deploy the server project, right-click on the *{artifactId}* project and choose *Run As* –&gt; *Run on Server*. A browser window appears that accesses the running application.
* To undeploy the project, right-click on the *{artifactId}* project and choose *Run As* –&gt; *Maven build*. Enter `wildfly:undeploy` for the *Goals* and click *Run*.
* Make sure you xref:restore_the_server_configuration[restore the {productName} server configuration] when you have completed testing this quickstart.

// Debug the Application
include::../shared-doc/debug-the-application.adoc[leveloffset=+1]
