Technologies used in this article :

  1. Neuro4j Workflows 2.0.1 (spring-mail-adapter plugin)
  2. Spring Integration (Mail)4.1.0.RC1
  3. Neuro4j Studio 1.0 (based on Eclipse Kepler)
  4. JDK 1.6

Expected result: Flow to receive email from Gmail.

Expected result

1. Download project.

Download project and unzip it.

Download java project – SpringEmailExample.zip

 

2. Import project into Studio.

3. Source code:

  • pom.xml - includes spring-mail-adapter plugin
    • spring-mail-adapter provides following blocks:
      • org.neuro4j.springintegration.mail.HandleInputMessage
      • org.neuro4j.springintegration.mail.debug.PrintMessageHeader
      • org.neuro4j.springintegration.mail.debug.PrintMessage
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.neuro4j.workflow.tutorial</groupId>
	<version>1.0-SNAPSHOT</version>
	<name>SpringEmailExample</name>
	<properties>
		<java-version>1.6</java-version>
		<org.springframework-version>4.1.0.RC1</org.springframework-version>
		<org.aspectj-version>1.8.1</org.aspectj-version>
		<logback.version>1.0.13</logback.version>
		<org.neuro4j-version>2.0.1</org.neuro4j-version>
	</properties>
	<repositories>
		<repository>
			<id>neuro4j-maven-milestone</id>
			<name>Neuro4J Maven Repository</name>
			<url>http://static.neuro4j.org/maven2</url>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>neuro4j-plugin-repo</id>
			<name>Neuro4j Maven Plugin Repository</name>
			<url>http://static.neuro4j.org/maven2</url>
		</pluginRepository>
	</pluginRepositories>

	<artifactId>SpringEmailExample</artifactId>

	<dependencies>
		<dependency>
			<groupId>org.neuro4j</groupId>
			<artifactId>neuro4j-workflow-common</artifactId>
			<version>${org.neuro4j-version}</version>
		</dependency>
		<dependency>
			<groupId>org.neuro4j</groupId>
			<artifactId>spring-mail-adapter</artifactId>
			<version>${org.neuro4j-version}</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>${logback.version}</version>
		</dependency>
	</dependencies>

	<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.n4j</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*</include>
				</includes>
			</resource>
		</resources>

		<plugins>

			<plugin>
				<groupId>org.neuro4j.tools</groupId>
				<artifactId>neuro4j-compiler-plugin</artifactId>
				<version>1.0</version>
				<executions>
					<execution>
						<id>generate</id>
						<goals>
							<goal>generate</goal>
							<goal>testGenerate</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

</project>
  • Spring configuration file gmail-pop3-config.xml - /SpringEmailExample/src/main/resources/META-INF/spring/integration/gmail-pop3-config.xml
  • Setup your login and password - use %40gmail.com instead of @gmail.com
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:beans="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
		http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
	xmlns:int="http://www.springframework.org/schema/integration"
	xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
	xmlns:util="http://www.springframework.org/schema/util">

	<int:channel id="receiveChannel" />

	<int-mail:inbound-channel-adapter id="pop3ShouldDeleteTrue"
		store-uri="pop3://login%40gmail.com:password@pop.gmail.com/INBOX"
		channel="receiveChannel" should-delete-messages="false" auto-startup="true"
		java-mail-properties="javaMailProperties">
		<int:poller fixed-rate="100000" />

	</int-mail:inbound-channel-adapter>

	<beans:import resource="controllers.xml" />
</beans>
  • Spring configuration file controllers.xml - /SpringEmailExample/src/main/resources/META-INF/spring/integration/controllers.xml
  • pass parameter org.neuro4j.springframework.email.example.GetMailsFromGmail-HandleMessage into factory method initMessageHandler
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
		http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
	xmlns:int="http://www.springframework.org/schema/integration"
	xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
	xmlns:util="http://www.springframework.org/schema/util">

	
<bean id="triggerFactory" class="org.neuro4j.springintegration.mail.MessageHandlerFactoryBean"/>

<bean id="getMailTrigger" factory-bean="triggerFactory" factory-method="initMessageHandler">
  <constructor-arg><ref bean="receiveChannel"/></constructor-arg>
  <constructor-arg><value>org.neuro4j.springframework.email.example.GetMailsFromGmail-HandleMessage</value></constructor-arg>
</bean>

	<util:properties id="javaMailProperties">
		<prop key="mail.pop3.socketFactory.fallback">false</prop>
		<prop key="mail.debug">true</prop>
		<prop key="mail.pop3.port">995</prop>
		<prop key="mail.pop3.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
		<prop key="mail.pop3.socketFactory.port">995</prop>
	</util:properties>
</beans>
  • Workflow:
    • HandleMessage - will be called by Spring once new mail received;
    • PrintMessageHeader - prints information from message's header;
    • PrintMessageInfo - prints information from message's body;

Expected result

  • java client code to run Spring - /SpringEmailExample/src/main/java/org/neuro4j/springframework/email/example/GmailPop3TestApp.java
        
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package org.neuro4j.springframework.email.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class GmailPop3TestApp {

	public static void main (String[] args) throws Exception {
		ApplicationContext ac = new ClassPathXmlApplicationContext("/META-INF/spring/integration/gmail-pop3-config.xml");
	}
}

 

4. Build project.

mvn clean package

5. Run Application.

mvn exec:java -Dexec.mainClass="org.neuro4j.springframework.email.example.GmailPop3TestApp"
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SM
PSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle],
com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLSt
re=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,i
ap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Ora
le]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail
Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransp
rt,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun
mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
EBUG: successfully loaded resource: /META-INF/javamail.default.address.map
EBUG: getProvider() returning javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]

Download Source Code

Download java project – SpringEmailExample.zip