
Email is very basic communication chanel between the business units. Integrating Email to enterprise application becomes the common business requirement for every enterprise application. Wildfly application provides many resource management function, email resource is also included inside resource management facility inside Wildfly. In this post, I will demonstrate how to create email resource and send email. For mail resource, I will demonstrate how to create SMTP and IMAP. Once I have finished creating resource, I will utilize the resource to send and receive email.
Table Contents
Below is what the contents of this post has be arrange
- Create Email Resource On Wildfly
- Configuring Email Reource
- Sending Email by utilizing Wildfly's email resource
- Reference
My environment to run the application is set up as following
- OS : Ubuntu 20 LTS
- Middle ware : OpenJDK 8, Wildfly Application Server 10.1.0
1.Create Email Resource on Wildfly
Creating email in wildfly is very easy. First, you need to log in to Wildfly management console
-
First, log in to the management console and go to configuration menu
Login to Management Console Go to Configuration Menu -
Go to Mail and add resource, then you need to create resource name and JNDI's name.
Go to Mail Resource Add mail resource Create resource and JNDI name -
Go inside mail resource and add mail servers
View Mail Resource Add SMTP server input SMTP connection detail Create and input connection detail for IMAP
2.Configuring Email Resource in standalone.xml
Once finishing create Email Reource on the management console, you need to configure email resource in standalone.xml file. For SMTP, you need to set STARTLS. And for IMAP, you need to enable SSL.
<subsystem xmlns="urn:jboss:domain:mail:2.0">
<mail-session name="default" jndi-name="java:jboss/mail/imaps">
<smtp-server outbound-socket-binding-ref="mail-imaps" ssl="true" username="your_email@hotmail.com" password="P@ssw0rd"/>
</mail-session>
<mail-session name="java-mail" jndi-name="java:/mail/smtp">
<smtp-server outbound-socket-binding-ref="mail-smtp" ssl="false" tls="true" username="your_email@hotmail.com" password="P@ssw0rd"/>
</mail-session>
</subsystem>
Then you need to bind socket port with actual SMTP and IMAP server
<socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:0}">
<socket-binding name="management-http" interface="management" port="${jboss.management.http.port:9990}"/>
<socket-binding name="management-https" interface="management" port="${jboss.management.https.port:9993}"/>
<socket-binding name="ajp" port="${jboss.ajp.port:8009}"/>
<socket-binding name="http" port="${jboss.http.port:8080}"/>
<socket-binding name="https" port="${jboss.https.port:8443}"/>
<socket-binding name="iiop" interface="unsecure" port="3528"/>
<socket-binding name="iiop-ssl" interface="unsecure" port="3529"/>
<socket-binding name="txn-recovery-environment" port="4712"/>
<socket-binding name="txn-status-manager" port="4713"/>
<socket-binding name="app-port" port="1881" fixed-port="false"/>
<outbound-socket-binding name="mail-smtp">
<remote-destination host="smtp-mail.outlook.com" port="587"/>
</outbound-socket-binding>
<outbound-socket-binding name="mail-imaps">
<remote-destination host="outlook.office365.com" port="993"/>
</outbound-socket-binding>
</socket-binding-group>
Now you need to restart your Wildfly application server. Once your application server started mail resource is available for you.
3. Sending and Receiving mail by utilizing mail resource
Once finishing configue standalone, you can send or receive email by using mail resource. First, I will demonstrate how to send email through mail resource.
To send an email just simply create an EJB as following.
package pro.tikkwiki.ee.mail;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* Session Bean implementation class JavaSendMail
*/
@Stateless(mappedName = "javaSendMail")
@LocalBean
public class JavaSendMail {
@Resource(name = "java:jboss/mail/smtp")
private Session session;
/**
* Default constructor.
*/
public JavaSendMail() {
super();
}
public void send(String addresses, String topic, String textMessage) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("myemail@hotmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(addresses));
message.setSubject(topic);
message.setText(textMessage);
Transport.send(message);
} catch (MessagingException e) {
Logger.getLogger(JavaSendMail.class.getName()).log(Level.WARNING, "Cannot send mail", e);
}
}
}
Next, to receive an email we need to create email receive EJB as below.
package pro.tikkwiki.ee.mail;
import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
/**
* Session Bean implementation class JaveRecieveMail
*/
@Stateless(mappedName = "javaRecieveMail")
@LocalBean
public class JaveRecieveMail {
@Resource(name = "java:jboss/mail/imaps")
private Session session;
/**
* Default constructor.
*/
public JaveRecieveMail() {
super();
}
public void receive() {
try {
Store store = session.getStore("imaps");
store.connect("outlook.office365.com","your_email@hotmail.com", "P@assw0rd");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message[] messages = inbox.getMessages();
for (int i = 0; i < messages.length; ++i) {
System.out.println("MESSAGE #" + (i + 1) + ":");
Message msg = messages[i];
System.out.println("Subject :" + msg.getSubject());
if(i==10) break;
}
inbox.close(false);
store.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
To test this code, I will use Arquilian to create test classes.
Create Test Case Project |
For the test class, I have created as below.
package pro.tikkwiki.blog.mail.web.test;
import javax.ejb.EJB;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import junit.framework.Assert;
import pro.tikkwiki.ee.mail.JavaSendMail;
import pro.tikkwiki.ee.mail.JaveRecieveMail;
@RunWith(Arquillian.class)
public class ApplicationUnitTest {
@Deployment
public static Archive<?> createTestArchive() {
return ShrinkWrap.create(WebArchive.class, "ApplicationUntitTest.war")
.addClass(JavaSendMail.class)
.addClass(JaveRecieveMail.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}
@EJB
private JavaSendMail sendMail;
@EJB
private JaveRecieveMail recieveMail;
@Test
public void test01() {
sendMail.send("your_email@hotmail.com", "test email 002", "Hello Test Test Test");
Assert.assertTrue(true);
}
@Test
public void test02() {
recieveMail.receive();
Assert.assertTrue(true);
}
}
Once we run the program, we will get as below result
Result for Run Test Cases |
No comments:
Post a Comment