Maven dependency
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mail</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.6</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-amqp</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mail</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.6</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-amqp</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
Gateway interface
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.annotation.Payload;
import org.springframework.integration.mail.MailHeaders;
public interface SendMail {
@Gateway(requestChannel = "input")
void send(@Header(MailHeaders.FROM) String from, @Header(MailHeaders.TO) String to, @Header(MailHeaders.SUBJECT) String subject, @Payload String message);
}
import org.springframework.integration.annotation.Header;
import org.springframework.integration.annotation.Payload;
import org.springframework.integration.mail.MailHeaders;
public interface SendMail {
@Gateway(requestChannel = "input")
void send(@Header(MailHeaders.FROM) String from, @Header(MailHeaders.TO) String to, @Header(MailHeaders.SUBJECT) String subject, @Payload String message);
}
Spring configuration
<?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:int="http://www.springframework.org/schema/integration" xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
xmlns:int-amqp="http://www.springframework.org/schema/integration/amqp" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/integration/amqp http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd
http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<rabbit:connection-factory id="rabbitConnectionFactory" host="xxx.xxx.xxx.xxx" virtual-host="/linus" username="linus_chien" password="xxx" />
<int-amqp:channel id="input" connection-factory="rabbitConnectionFactory" queue-name="linus.test" />
<int-mail:outbound-channel-adapter id="mailAdaptor" channel="input" host="xxx.xxx.xxx.xxx" />
<int:gateway id="sendMail" service-interface="SendMail" />
</beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration" xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
xmlns:int-amqp="http://www.springframework.org/schema/integration/amqp" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/integration/amqp http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd
http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<rabbit:connection-factory id="rabbitConnectionFactory" host="xxx.xxx.xxx.xxx" virtual-host="/linus" username="linus_chien" password="xxx" />
<int-amqp:channel id="input" connection-factory="rabbitConnectionFactory" queue-name="linus.test" />
<int-mail:outbound-channel-adapter id="mailAdaptor" channel="input" host="xxx.xxx.xxx.xxx" />
<int:gateway id="sendMail" service-interface="SendMail" />
</beans>
Test Case
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SpringIntegrationTest {
@Autowired
private SendMail sendMail;
@Test
public void test() {
sendMail.send("linus_chien@xxx.xxx.xxx.xxx", "linus_chien@xxx.xxx.xxx.xxx", "Test", "This is a gateway annotation test.");
}
}
就這樣沒寫什麼程式就可以發信了。
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SpringIntegrationTest {
@Autowired
private SendMail sendMail;
@Test
public void test() {
sendMail.send("linus_chien@xxx.xxx.xxx.xxx", "linus_chien@xxx.xxx.xxx.xxx", "Test", "This is a gateway annotation test.");
}
}