1,applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="mail.properties" />
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}" />
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
</bean>
<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="${mail.username}" />
</bean>
<bean id="orderManager"
class="ivan.test.email.springemail.action.impl.SimpleOrderManager">
<property name="mailSender" ref="mailSender" />
<property name="templateMessage" ref="templateMessage" />
</bean>
</beans>
2,实体类Order
package ivan.test.email.springemail.entity;
public class Order {
private Customer customer;
private int orderNumber;
private String subject;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public int getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(int orderNumber) {
this.orderNumber = orderNumber;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
3,实体类Customer
package ivan.test.email.springemail.entity;
public class Customer {
private String firstName;
private String lastName;
private String emailAddress;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
4,发邮件接口
package ivan.test.email.springemail.action;
import ivan.test.email.springemail.entity.Order;
public interface OrderManager {
void placeOrder(Order order);
}
5,发邮件实现类
package ivan.test.email.springemail.action.impl;
import ivan.test.email.springemail.action.OrderManager;
import ivan.test.email.springemail.entity.Order;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
public class SimpleOrderManager implements OrderManager {
private MailSender mailSender;
private SimpleMailMessage templateMessage;
public void placeOrder(Order order) {
SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
msg.setTo(order.getCustomer().getEmailAddress());
msg.setText("Dear" + order.getCustomer().getFirstName()
+ order.getCustomer().getLastName()
+ ", thank you for placing order. Your order number is"
+ order.getOrderNumber());
msg.setSubject(order.getSubject());
try {
this.mailSender.send(msg);
} catch (MailException ex) {
System.err.println(ex.getMessage());
}
}
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public void setTemplateMessage(SimpleMailMessage templateMessage) {
this.templateMessage = templateMessage;
}
}
6,测试类
package ivan.test.email.springemail;
import ivan.test.email.springemail.action.OrderManager;
import ivan.test.email.springemail.entity.Customer;
import ivan.test.email.springemail.entity.Order;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext.xml" })
public class SimpleOrderManagerTest {
@Resource
private OrderManager orderManager;
@Test
public void test() {
Order order = new Order();
Customer customer = new Customer();
customer.setEmailAddress("43811702@qq.com");
customer.setFirstName("Ivan");
customer.setLastName("Zero");
order.setCustomer(customer);
order.setOrderNumber(10011);
order.setSubject("My Order");
orderManager.placeOrder(order);
}
}