Skip to main content

Configure Seam Mail

From time to time we have need to send an e-mail from application. This task should not be so complicated, but if you do it for the first time you can get some troubles and it can take you some time. Because of that I will try to explain how you can configure it correctly.


Basics about Seam mail


Seam includes components for sending and templating mails.
To include this components in project jboss-seam-mail.jar file is required, which contains mail JSF controls and mailSession componet.
To include mail support in maven project we need to add next dependency:

<dependency>
    <groupId>org.jboss.seam</groupId>
    <artifactId>jboss-seam-mail</artifactId>
</dependency>


<!-- This dependency is optional, it is required if you want manually set mailSession. -->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.1</version>
</dependency>

If you use Tomcat as your application server you need to include mail.jar file in lib directory of Tomcat. If you use JBoss as your application server, the necessary mail JavaMailAPI implementation is already there.

The Seam Email module requires the use of Facelets as the view technology. Additionally, it requires the use of the seam-ui package.


Configuring mailSession:

To configure mailSession component we can use componets.xml file for that purpose, or we can do it programmatically.

For configuration of mailSession we can use JNDI look up.

JNDI lookup:

Configuration of mailSession using components.xml for lookup information about mail server settings.

<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:comp/env/mail/MailServer"/> 

</components>

Or if we want to do it programmatically:

@In(scope = ScopeType.Application, required = false)
private MailSession mailSession;


.....


@Create
public void init(){
    .....
    if (mailSession == null) {
// Read mail configuration from properties file. Or it can be hardcoded.
Properties mailProperties = new Properties();
mailProperties.load(this.getClass().getResourceAsStream("/mail_config.properties"));


mailSession = new MailSession();


mailSession.setSessionJndiName("java:comp/env/mail/MailServer");


mailSession.create();
    }
    ....
}

For JBoss:

The JBossAS deploy/mail-service.xml configures a JavaMail session binding into JNDI.

<attribute name="mail/MailServer">        
    <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="nobody"></property>


           <!-- Change to the mail server  -->
           <property name="mail.pop3.host" value="pop3.nosuchhost.nosuchdomain.com"></property>


           <!-- Enable debugging output from the javamail classes --> 
           <property name="mail.debug" value="false"></property>      


    </configuration>
</attribute>

For Tomcat:

Or if we use tomcat we need to configure resurce in server.xml under global resource:

  <Resource  name="mail/MailServer"
         auth="Container"
         type="javax.mail.Session"
         mail.transport.protocol="smtp"
         mail.smtp.host="localhost"
         mail.smtp.port="25"
         mail.smtp.user="userName@localhost"
         password="password" />

Without JNDI lookup:

If we don't need to use JNDI look up to configure mailSeesion. We can do it in components.xml or programmatically.

Using components.xml:

<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 host="localhost" password="password" username="userName" port="25"/> 

</components>

Programmatically:

@In(scope = ScopeType.Application, required = false)
private MailSession mailSession;


.....


@Create
public void init(){
    .....
    if (mailSession == null) {
// Read mail configuration from properties file. Or it can be hardcoded.
Properties mailProperties = new Properties();
mailProperties.load(this.getClass().getResourceAsStream("/mail_config.properties"));


mailSession = new MailSession();


mailSession.setHost(mailProperties.getProperty("host"));
mailSession.setUsername(mailProperties.getProperty("userName"));
mailSession.setPassword(mailProperties.getProperty("password"));
mailSession.setPort(Integer.valueOf(mailProperties.getProperty("smtpPort")));
mailSession.setTransport("smtp");


mailSession.create();
    }
    ....
}

peroperties file:

userName=userName@localhost
password=password
host=localhost
transport=smtp
pop3Port=
smtpPort=25


Comments

Popular posts from this blog

Checking file's "magic numbers"

Few days ago I had very interesting task. Our customer required that we perform checking of so called file's "magic numbers" to determinate does uploaded file correspond to it's extension.  We are already allowed only to upload files with some predefined extensions (PDF, DOC ...). But this can not prevent some evil user to update an exe file after renaming it to PDF or DOC. So first of all I will explain what are "magic numbers", and then I will show how we handle them.

Running Spring Boot Web App on the Random Port from Port Range

By default the spring boot web application is listening on the port 8080 for the incoming connection. This behavior can be changed by providing server.port property value during starting of the application or as part of the application.properties or through the code by implementing EmbeddedServletContainerCustomizer. But it would be even better if we could specified a range of the ports which can be used for the starting the application. It would be great if I could specify a property like server.portRange=8100..8200 to define a list of the port on which I want to start my service. In this blog post I will describe how this can be done.

Simple Workflow Engine With Spring

Few months ago, during working on one of the company project, we had need to developed  REST services which is used for sending an email depending on data sent by client application. During developing this service we decide to create simple workflow engine which will be charged for sending an email, but also this engine can be used for any kind of simple flows. In this article i will explain step by step how you can implement your simple workflow engine which can handle sequence flow.