`
InvocationHandler
  • 浏览: 36326 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

初学Maven小例子--使用greenmail测试邮件

阅读更多
利用Maven构建的一个小工程,闲话少叙,直入正题:
1、利用mvn archetype:generate生成基本的项目骨架
2、修改pom.xml:
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.maven.account</groupId>
  <artifactId>account-email</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Account Email</name>
  <packaging>jar</packaging>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<springframework.version>2.5.6</springframework.version>
  </properties>

  <dependencies>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<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>
		<groupId>com.icegreen</groupId>
		<artifactId>greenmail</artifactId>
		<version>1.3.1b</version>
		<scope>test</scope>
	</dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.7</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

3、写接口AccountEmailService.java
package com.maven.account;

/**
 * @Author:liuxb
 * @Date: 2012-08-04
 */
public interface AccountEmailService 
{
    void sendMail(String to,String subject,String htmlText) throws Exception;
}

4、写实现类AccountEmailServiceImpl.java
package com.maven.account;

/**
 * @Author:liuxb
 * @Date: 2012-08-04
 */

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

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

public class AccountEmailServiceImpl implements AccountEmailService
{
	private JavaMailSender javaMailSender;

	private String systemEmail;
	
	public void sendMail(String to,String subject,String htmlText) throws Exception{
		try{
			MimeMessage msg = javaMailSender.createMimeMessage();
			MimeMessageHelper msgHelper = new MimeMessageHelper(msg);

			msgHelper.setFrom(systemEmail);
			msgHelper.setTo(to);
			msgHelper.setSubject(subject);
			msgHelper.setText(htmlText,true);

			javaMailSender.send(msg);
		}catch(MessagingException e){
			e.printStackTrace();
		}
	}

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

5、在src目录下新建文件夹resources
6、在resources目录下建立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>

7、在resources目录下建立service.properties文件
email.protocol=smtp
email.host=localhost
email.port=25
email.username=test@maven.com
email.password=123456
email.auth=true
email.systemEmail=liuxb@maven.com

8、建立测试类AccountEmailServiceTest.java
package com.maven.account;

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>Test</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();
	}
}

9、在项目根目录下使用命令mvn clean test进行测试。
输出如下:




10、使用命令mvn dependency:tree查看依赖树:


  • 描述: 测试结果图1
  • 大小: 82.6 KB
  • 描述: 测试结果图2
  • 大小: 89.6 KB
  • 描述: 依赖树图片
  • 大小: 65 KB
0
2
分享到:
评论
1 楼 liubao20104091 2014-03-04  
为什么我的说权限不够无法启动服务啊

相关推荐

    apache-maven-3.6.3-bin

    apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-...

    apache-maven-3.9.6-bin

    例如,maven-compiler-plugin用于编译Java源码,maven-surefire-plugin用于执行单元测试,maven-jar-plugin则负责创建JAR文件。 在实际开发中,Maven的`settings.xml`文件通常放在用户的主目录下的`.m2`目录下,它...

    maven资源 apache-maven-3.3.9-bin.zip

    每个阶段都可以通过特定的插件来执行,例如,`maven-compiler-plugin`用于编译源代码,`maven-surefire-plugin`负责运行单元测试。Maven通过使用Project Object Model (POM)文件来描述项目信息,包括依赖、构建配置...

    maven-deploy-plugin-2.8.2.jar

    maven-deploy-plugin-2.8.2.jar

    apache-maven-3.8.6.zip

    apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-...

    maven-archetype-quickstart-1.1.jar包下载

    `maven-archetype-quickstart` 就是这样一个archetype,它提供了一个简单的Java应用结构,包含了一个主类和一个测试类,适合初学者或者快速原型开发。 **快速启动Archetype的使用步骤:** 1. **安装Archetype**:...

    apache-maven-3.8.4-bin.zip maven下载鬼慢

    Maven还支持插件机制,允许扩展其功能,例如,你可以使用maven-javadoc-plugin生成API文档,maven-surefire-plugin执行单元测试,或者maven-site-plugin生成项目网站。在pom.xml中定义插件及其配置,即可实现这些...

    apache-maven-3.9.0-bin.tar

    Maven的插件系统也是其强大之处,通过安装额外的插件,可以支持更多的构建任务,如生成源码文档(javadoc)、执行单元测试(maven-surefire-plugin)、打包成不同格式(如JAR、WAR或EAR)等。 "linux版本maven"指的...

    maven集成jetty所需jar包maven-jetty-plugin,多版本

    - 使用旧版本的`maven-jetty-plugin`可能会导致某些新特性无法使用或与其他库冲突,因此保持插件更新是很重要的。 6. 结论: `maven-jetty-plugin`简化了Java Web应用的开发和测试过程,通过Maven的命令行即可...

    maven-jar-plugin-3.1.1.jar

    maven-jar-plugin-3.1.1.jar

    maven-install-plugin-2.4.jar

    maven-install-plugin-2.4.jar

    maven-eclipse-plugin

    6. 使用`maven-插件.zip`中的其他插件:这个压缩包可能包含了其他Maven插件,它们可以帮助解决特定的问题,例如代码格式化、依赖管理和项目清理等。 总之,maven-eclipse-plugin是连接Maven和Eclipse的桥梁,让...

    apache-maven-3.5.4-bin.tar.gz

    例如,使用maven-surefire-plugin进行单元测试,maven-javadoc-plugin生成API文档,maven-source-plugin打包源代码。在`pom.xml`中定义插件并配置相关参数,然后通过`mvn plugin:goal`执行。 总之,Apache Maven ...

    maven jar包

    maven-aether-provider-3.2.1-sources.jar maven-antrun-plugin-1.3.jar maven-archiver-2.2.jar maven-artifact-3.2.1-sources.jar maven-assembly-plugin-2.2-beta-5.jar maven-bundle-plugin-1.0.0.jar maven-...

    最新Maven-apache-maven-3.8.1-Repository-配置好的

    - **插件**: Maven的扩展性主要体现在插件上,如`maven-compiler-plugin`用于编译Java代码,`maven-surefire-plugin`用于执行单元测试。 - **版本控制**: Maven遵循严格的版本号规则,例如`1.0.0-SNAPSHOT`表示开发...

    maven-assembly-plugin-2.2-beta-5.jar

    maven-assembly-plugin-2.2-beta-5.jar

    apache-maven-3.5.3-src

    例如,maven-jar-plugin用于创建JAR文件,maven-surefire-plugin用于运行单元测试。在"apache-maven-3.5.3-src"中,你会看到各种插件的源代码,这有助于理解它们的工作原理并自定义它们的行为。 总的来说,Apache ...

    maven-deploy-plugin-2.7.jar

    maven-deploy-plugin-2.7.jar

    maven-surefire-plugin-2.22.1.jar

    maven-surefire-plugin-2.22.1.jar

Global site tag (gtag.js) - Google Analytics