`
weigang.gao
  • 浏览: 491357 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

account-email(5)

 
阅读更多

Maven项目(account-email)的目结构如下:

account-email项目Java主代码位于src/main/java,资源文件(非Java文件)位于src/main/resources目录下。

 

1.pom.xml

<?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/maven-v4_0_0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
	<!--实际项目-->
    <groupId>com.juvenxu.mvnbook.account</groupId>
	<!--Maven项目,所谓Maven项目就是实际项目中的一个模块-->
    <artifactId>account-email</artifactId>
	<!--Maven项目版本,SNAPSHOT说明还不是稳定版本-->
    <version>1.0.0-SNAPSHOT</version>
	<!--name不是必须的,声明一个对于用户更为友好的项目名称-->
    <name>Account Email</name>
	<!--定义Maven的打包方式-->
    <packaging>jar</packaging>
    
    <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	   <!--对于acount-email中这些Spring Framework来说,也应该在一个唯一的地方定义版本,并且在dependency声明中引用这一版本。这样,在升级Spring Framework的时候就只需要修改一处-->
       <springframework.version>2.5.6</springframework.version>
    </properties>

    <dependencies>
    	<dependency>
    		<groupId>org.springframework</groupId>
    		<artifactId>spring-core</artifactId>
    		<!--<version>2.5.6</version>-->
			<!--引用在properties中定义的版本信息-->
			<version>${springframework.version}</version>
    	</dependency>
    	<dependency>
    		<groupId>org.springframework</groupId>
    		<artifactId>spring-beans</artifactId>
    		<version>${springframework.version}</version>
    	</dependency>
	
    	<dependency>
    		<groupId>org.springframework</groupId>
    		<artifactId>spring-context</artifactId>
    		<version>${springframework.version}</version>
    	</dependency>
		
    	<dependency>
    		<groupId>org.springframework</groupId>
    		<artifactId>spring-context-support</artifactId>
    		<version>${springframework.version}</version>
    	</dependency>
		
    	<dependency>
		    <!--实现发送必须的类库-->
    		<groupId>javax.mail</groupId>
    		<artifactId>mail</artifactId>
    		<version>1.4.1</version>
    	</dependency>
    	<dependency>
		    <!--Junit是Java社区事实上的单元测试标准-->
    		<groupId>junit</groupId>
    		<artifactId>junit</artifactId>
    		<version>4.7</version>
    		<scope>test</scope>
    	</dependency>
    	<dependency>
		    <!--GreenMail开源的邮件服务测试套件-->
    		<groupId>com.icegreen</groupId>
    		<artifactId>greenmail</artifactId>
                <version>1.3.1b</version>
    		<scope>test</scope>
    	</dependency>
   </dependencies>
</project>

 

2.AccountEmailService.java接口

package com.newtouch.mvnbook.account.email;

public interface AccountEmailService {
	/**sendMail()方法用来发送html格式的邮件,to为接收地址,subject为邮箱主题,htmlText为邮箱内容,
	 * 如果发送邮箱出错,则抛出AccountEmailException异常。
	 * 
	 * @param to
	 * @param subject
	 * @param htmlText
	 * @throws AccountEmailException
	 */
	public void sendMail(String to, String subject, String htmlText) throws AccountEmailException;

}

 

3.接口的实现AccountEmailServiceImpl

package com.newtouch.mvnbook.account.email;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class AccountEmailServiceImpl implements AccountEmailService{

	private JavaMailSender javaMailSender;
	
	private String systemEmail;
	
	@Override
	public void sendMail(String to, String subject, String htmlText)
			throws AccountEmailException {
		
		
		
		    try {
				MimeMessage msg = javaMailSender.createMimeMessage();
				MimeMessageHelper msgHelper = new MimeMessageHelper(msg);
				
				msgHelper.setFrom(systemEmail);
				msgHelper.setTo(to);
				msgHelper.setSubject(subject);
				msgHelper.setText(htmlText, true);//true表示邮箱的内容为html格式
				javaMailSender.send(msg);
			} catch (MessagingException e) {
				throw new AccountEmailException("Fail to send email.", e);
				
			}
	
		
	}

	public JavaMailSender getJavaMailSender() {
		return javaMailSender;
	}

	public void setJavaMailSender(JavaMailSender javaMailSender) {
		this.javaMailSender = javaMailSender;
	}

	public String getSystemEmail() {
		return systemEmail;
	}

	public void setSystemEmail(String systemEmail) {
		this.systemEmail = systemEmail;
	}

}

 

4.自定义异常AccountEmailException

package com.newtouch.mvnbook.account.email;

public class AccountEmailException extends Exception {
	
	public AccountEmailException(String msg, Throwable cause) {

		super(msg, cause);

	}
}

 

5.spring配置文件account-email.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"  
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="location" value="classpath:service.properties" />  
    </bean>  
  
    <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">  
        <property name="protocol" value="${email.protocol}" />  
        <property name="host" value="${email.host}" />  
        <property name="port" value="${email.port}" />  
        <property name="username" value="${email.username}" />  
        <property name="password" value="${email.password}"/>  
        <property name="javaMailProperties">  
            <props>  
                <prop key="mail.${email.protocol}.auth">${email.auth}</prop>  
            </props>  
        </property>  
    </bean>  
  
    <bean id="accountEmailService" class="com.maven.account.AccountEmailServiceImpl">  
        <property name="javaMailSender" ref="javaMailSender" />  
        <property name="systemEmail" value="${email.systemEmail}" />  
    </bean>  
  
</beans> 

 

6.属性文件service.properties

email.protocol=smtp
#使用localhost不一定可以,换成127.0.0.1
#email.host=localhost  
email.host=127.0.0.1
email.port=25  
email.username=test@maven.com  
email.password=123456  
email.auth=true  
email.systemEmail=liuxb@maven.com  

 

7.编写测试类AccountEmailServiceTest

package com.newtouch.mvnbook.account.email;

import static junit.framework.Assert.assertEquals;  

import javax.mail.Message;  
import org.junit.After;  
import org.junit.Before;  
import org.junit.Test;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
import com.icegreen.greenmail.util.GreenMail;  
import com.icegreen.greenmail.util.GreenMailUtil;  
import com.icegreen.greenmail.util.ServerSetup;  
  
//import junit.framework.Test;  
//import junit.framework.TestCase;  
//import junit.framework.TestSuite;  
  
/** 
 * Unit test for simple App. 
 */  
public class AccountEmailServiceTest   
{  
    private GreenMail greenMail;  
  
    @Before  
    public void startMailServer() throws Exception{  
        greenMail = new GreenMail(ServerSetup.SMTP);  
        greenMail.setUser("test@maven.com","123456");  
        greenMail.start();  
    }  
  
    @Test  
    public void testSendMail() throws Exception{  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("account-email.xml");  
        AccountEmailService accountEmailService = (AccountEmailService)ctx.getBean("accountEmailService");  
  
        String subject = "Test Subject";  
        String htmlText = "<h3>gaoweigang</h3>";  
        accountEmailService.sendMail("test2@maven.com",subject,htmlText);  
  
        greenMail.waitForIncomingEmail(2000,1);  
  
        Message[] msgs = greenMail.getReceivedMessages();  
        System.out.println("msgs.length="+msgs.length);  
        System.out.println("subject="+msgs[0].getSubject());  
        System.out.println("htmlText="+GreenMailUtil.getBody(msgs[0]).trim());  
  
        assertEquals(1,msgs.length);  
        assertEquals(subject,msgs[0].getSubject());  
        assertEquals(htmlText,GreenMailUtil.getBody(msgs[0]).trim());  
    }  
  
    @After  
    public void stopMailServer() throws Exception{  
        greenMail.stop();  
    }  
}  

 

 

 

 

 

  • 大小: 13.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics