Spring 发送邮件--简单发送消息

订单:

package com.webservice.model;

import java.util.Date;

public class Order {

	private int id;

	private Date orderDate;

	private int orderNumber;

	private Customer customer;

	private String msg;

	private double price;

	public Customer getCustomer() {
		return customer;
	}

	public int getId() {
		return id;
	}

	public Date getOrderDate() {
		return orderDate;
	}

	public int getOrderNumber() {
		return orderNumber;
	}

	public double getPrice() {
		return price;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setOrderDate(Date orderDate) {
		this.orderDate = orderDate;
	}

	public void setOrderNumber(int orderNumber) {
		this.orderNumber = orderNumber;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}

顾客:

package com.webservice.model;

public class Customer {

	private int id;
	private String firstName;
	private String lastName;
	private String emailAddress;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	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;
	}

}

控制类:

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.webservice.mail.support.SimpleOrderManager;
import com.webservice.model.Customer;
import com.webservice.model.Order;

@Controller
public class SenderMailAction {

	@Autowired
	@Qualifier("orderManager")
	SimpleOrderManager simpleOrderManager;

	@RequestMapping(value = "/senderMail", method = RequestMethod.POST)
	public String sender(Model model, HttpServletRequest request) {

		String address = request.getParameter("address");
		String msg = request.getParameter("msg");
		Order order = new Order();
		Customer customer = new Customer();
		customer.setEmailAddress(address);
		order.setCustomer(customer);
		order.setId(1);
		order.setMsg(msg);
		order.setOrderDate(new Date());
		order.setOrderNumber(12);
		order.setPrice(123.32);
		simpleOrderManager.placeOrder(order);

		return "sender";
	}

发送邮件类:

package com.webservice.mail.support;

import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

import com.webservice.mail.OrderManager;
import com.webservice.model.Order;

public class SimpleOrderManager implements OrderManager {

	private MailSender mailSender;
	private SimpleMailMessage templateMessage;

	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void setTemplateMessage(SimpleMailMessage templateMessage) {
		this.templateMessage = templateMessage;
	}

	public void placeOrder(Order order) {

		// Do the business calculations...

		// Call the collaborators to persist the order...

		// Create a thread safe "copy" of the template message and customize it
		SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
		msg.setTo(order.getCustomer().getEmailAddress());
		msg.setText("Dear, thank you for placing order. Your order number is "
				+ order.getOrderNumber() + "⁢BR/>" + "infomation:"
				+ order.getMsg() + "price:" + order.getPrice());
		try {
			this.mailSender.send(msg);
		} catch (MailException ex) {
			// simply log it and go on...
			System.err.println(ex.getMessage());
		}
	}

}




spring-servlet.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"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:p="http://www.springframework.org/schema/p"
	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
						http://cxf.apache.org/jaxws
						http://cxf.apache.org/schemas/jaxws.xsd">

	<context:annotation-config />

	<context:component-scan base-package="com.webservice" />

	<!-- 自动识别aspectJ注解 -->
	<bean
		class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />

	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/view/" p:suffix=".jsp">
	</bean>


	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="smtp.163.com" />
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">true</prop>
				<prop key="mail.smtp.tiemout">25000</prop>
			</props>
		</property>
		<property name="username" value="gpodalian_edu" />
		<property name="password" value="*********" />
	</bean>

	<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
		<property name="from" value="gpodalian_edu@**.com" />
		<property name="subject" value="NOTICE" />
	</bean>

	<bean id="orderManager" class="com.webservice.mail.support.SimpleOrderManager">
		<property name="mailSender" ref="mailSender" />
		<property name="templateMessage" ref="templateMessage" />
	</bean>


</beans>




对邮件有点兴趣,感觉Spring做起来,花很少的时间就可以完成,效益很好。

version:spring3.0.5


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值