spring + mail
orderManagerImpl:
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; public class OrderManagerImpl implements OrderManager { private MailSender mailSender; private SimpleMailMessage templateMessage; public void placeOrder() { SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage); msg.setTo("[email protected]"); msg.setText("my first test mail."); //MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8"); try { this.mailSender.send(msg); } catch (MailException e) { e.printStackTrace(); System.out.println(e.getMessage()); } } /** * Setter method for property <tt>mailSender</tt>. * * @param mailSender value to be assigned to property mailSender */ public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; } /** * Setter method for property <tt>simpleMailMessage</tt>. * * @param simpleMailMessage value to be assigned to property simpleMailMessage */ public void setTemplateMessage(SimpleMailMessage templateMessage) { this.templateMessage = templateMessage; } }
sendMailTest.java
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * @author wb-yingpf * @version $Id: SendMailTest.java, v 0.1 2012-6-11 下午03:05:45 wb-yingpf Exp $ */ public class SendMailTest { @Test public void testSendMail() { ApplicationContext ac = new ClassPathXmlApplicationContext("test.xml"); OrderManagerImpl orderManager = (OrderManagerImpl) ac.getBean("orderManager"); orderManager.placeOrder(); } }
test.xml
<?xml version="1.0" encoding="GBK"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:sofa="http://img.alipay.net/dtd/schema/service" xmlns:context="http://www.springframework.org/schema/context" xmlns:webflow="http://www.springframework.org/schema/webflow-config" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://img.alipay.net/dtd/schema/service http://img.alipay.net/dtd/schema/service/sofa-service.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd" default-autowire="byName"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="defaultEncoding" value="utf-8"></property> <property name="host" value="smtp.163.com"></property> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> </props> </property> <property name="username" value="[email protected]"></property> <property name="password" value="*****"></property> </bean> <bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="[email protected]"></property> <property name="subject" value="[email protected]"></property> </bean> <bean id="orderManager" class="com.alipay.OrderManagerImpl"> <property name="mailSender" ref="mailSender" /> <property name="templateMessage" ref="templateMessage" /> </bean> </beans>
需要导入的jar包:
Spring-2.5.4.jar
junit-4.7.jar
commons-logging-1.1.1.jar
mail-1.4.jar