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
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
Post a Comment