1、GreenMail简介
GreenMail是一个用于测试的、开源的、直观的和易于使用的邮件服务器测试套件。它支持 SMTP、POP3、IMAP 等。它还为JBoss提供GreenMail服务。
2、GreenMail在实际项目中的使用
(1)项目的POM文件:
<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.jackie.codeproject.account</groupId>
<artifactId>account-email</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>account-email</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.2.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.3.1b</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
(2) 邮件发送接口:
/*
* Copyright 2013-2015
*/
package com.jackie.codeproject.account.email;
/**
* Title: AccountEmailService.java
* 邮件接口
*
* @author jackie
* @since Mar 30, 2013 5:58:07 PM
* @version V1.0
*/
public interface AccountEmailService {
/**
* 发送邮件方法
* @author jackie
* @date Mar 30, 2013
* @param to
* @param subject
* @param htmlText
* @throws Exception
* @return void
*/
void sendEmail(String to, String subject, String htmlText) throws AccountEmailException;
}
(2) 邮件发送接口实现类:
/*
* Copyright 2013-2015
*/
package com.jackie.codeproject.account.email;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
/**
* Title: AccountEmailServiceImpl.java
* 邮件发送的实现类
*
* @author jackie
* @since Mar 30, 2013 6:11:40 PM
* @version V1.0
*/
public class AccountEmailServiceImpl implements AccountEmailService {
/**
* @Fields javaMailSender : 简化邮件发送的工具类
*/
private JavaMailSender javaMailSender;
/**
* @Fields systemEmail : 系统邮箱账号
*/
private String systemEmail;
public void sendEmail(String to, String subject, String htmlText) throws AccountEmailException{
try {
// 使用javaMailSender创建一个MimeMessage msg,对应了将要发送的邮件
MimeMessage msg = javaMailSender.createMimeMessage();
// 通过MimeMessage创建一个MimeMessageHelper msgHelper
MimeMessageHelper msgHelper = new MimeMessageHelper(msg);
// 使用MimeMessageHelper设置邮件的发送地址、收件地址、主题以及内容
msgHelper.setFrom(systemEmail);
msgHelper.setTo(to);
msgHelper.setSubject(subject);
msgHelper.setText(htmlText, true);
// 发送邮件
javaMailSender.send(msg);
} catch (MessagingException e) {
throw new AccountEmailException("Failed to send email.", e);
}
}
/**
* @return the javaMailSender
*/
public JavaMailSender getJavaMailSender() {
return javaMailSender;
}
/**
* @param javaMailSender
* the javaMailSender to set
*/
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
/**
* @return the systemEmail
*/
public String getSystemEmail() {
return systemEmail;
}
/**
* @param systemEmail
* the systemEmail to set
*/
public void setSystemEmail(String systemEmail) {
this.systemEmail = systemEmail;
}
}
(4)自定义异常类:
/*
* Copyright 2013-2015
*/
package com.jackie.codeproject.account.email;
/**
* Title: AccountEmailException.java 自定义邮件异常
*
* @author jackie
* @since Mar 30, 2013 6:01:31 PM
* @version V1.0
*/
public class AccountEmailException extends Exception {
/**
* @Fields serialVersionUID
*/
private static final long serialVersionUID = -5399025975888079256L;
/**
* 错误消息
*/
private String message;
public AccountEmailException(String message) {
this.message = message;
}
public AccountEmailException(String message, Throwable throwable) {
super(message, throwable);
}
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message
* the message to set
*/
public void setMessage(String message) {
this.message = message;
}
}
(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-3.2.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:email.properties"></property>
</bean>
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="protocol" value="${email.protocol}"></property>
<property name="host" value="${email.host}"></property>
<property name="port" value="${email.port}"></property>
<property name="username" value="${email.username}"></property>
<property name="password" value="${email.password}"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.${email.protocol}.auth">${email.auth}</prop>
</props>
</property>
</bean>
<bean id="accountEmailService" class="com.jackie.codeproject.account.email.AccountEmailServiceImpl">
<property name="javaMailSender" ref="javaMailSender">
</property>
<property name="systemEmail" value="${email.systemEmail}"></property>
</bean>
</beans>
(6)属性文件email.properties:
#协议
email.protocol=smtp
#服务器
email.host=localhost
#端口
email.port=25
#用户名
email.username=test@gmail.com
#密码
email.password=123456
#服务器需要用户名和密码进行认证
email.auth=true
#系统邮箱
email.systemEmail=jackie@sina.com
(7)测试类:
/*
* Copyright 2013-2015
*/
package com.jackie.codeproject.account.email;
import static org.junit.Assert.*;
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;
/**
* Title: AccountEmailServiceTest.java 邮件服务测试类
*
* @author jackie
* @since Mar 30, 2013 6:13:14 PM
* @version V1.0
*/
public class AccountEmailServiceTest {
private GreenMail greenMail;
private ApplicationContext applicationContext;
/**
* 启动邮件服务器
*
* @author jackie
* @date Mar 30, 2013
* @throws Exception
* @return void
*/
@Before
public void setUp() throws Exception {
greenMail = new GreenMail(ServerSetup.SMTP);
greenMail.setUser("test@gmail.com", "123456");
greenMail.start();
}
/**
* Test method for
* {@link com.jackie.codeproject.account.email.AccountEmailService#sendEmail(java.lang.String, java.lang.String, java.lang.String)}
*
*/
@Test
public void testSendEmail() throws Exception {
applicationContext = new ClassPathXmlApplicationContext("account-email.xml");
AccountEmailService accountEmailService = (AccountEmailService) applicationContext.getBean("accountEmailService");
String subject = "Test Subject";
String htmlText = "<h3> Test </h3>";
accountEmailService.sendEmail("test2@gmail.com", subject, htmlText);
greenMail.waitForIncomingEmail(2000, 1);
Message[] msgs = greenMail.getReceivedMessages();
assertEquals(1, msgs.length);
assertEquals(subject, msgs[0].getSubject());
assertEquals(htmlText, GreenMailUtil.getBody(msgs[0]).trim());
}
/**
* 关闭邮件服务器
*
* @author jackie
* @date Mar 30, 2013
* @throws Exception
* @return void
*/
@After
public void tearDown() throws Exception {
greenMail.stop();
}
}
分享到:
相关推荐
GreenMail邮件客户端GreenMail邮件客户端
项目源码 博文链接:https://invocationhandler.iteye.com/blog/1621408
C#邮电源码GreenMail_C#邮电源码GreenMail_C#邮电源码GreenMail_C#邮电源码GreenMail
是用于测试目的的电子邮件服务器的开源,直观且易于使用的测试套件。 支持带有SSL套接字支持的SMTP,POP3,IMAP,并且可以作为独立Java应用程序或docker容器嵌入junit测试中运行。 GreenMail是第一个也是唯一一个...
实现语言:C# 环境要求:Visual Studio 2005, SQL Server 2005
GreenMail系统 ACCP6.0课程考试
【原创田超凡,未经许可请勿转发,侵权仿冒必究】
java源码 直接复制 jsp
C#做的小东西,简单的邮箱..........
GreenMail是用于测试目的的直观,易于使用的电子邮件服务器测试套件。 支持SMTP,POP3,具有SSL套接字支持的IMAP,以及可配置的用户帐户和凭据。 全部采用100%Java语言。 我们已移动到集线器:...
greenmail-1.3.1b
s1 复习资料 s1 复习资料
saga-lib.zip,guice集成com.codeboollets.saga-libjavasaga库来组织域事件。目标是组织由任何类型的消息触发的状态和活动。
【原创田超凡,未经许可请勿转发,侵权仿冒必究】
【原创田超凡,未经许可请勿转发,侵权仿冒必究】
Java测试生态系统的完整概述 该存储库包含每个Java开发人员都必须知道的《 30个测试工具和库》一书的源代码。 您的副本。 书的目标 丰富您现有的测试工具箱 使用正确的工具完成工作 通知有关Java测试生态系统的...
accp5.0 s1结业考试 机试(带答案) 2008 GreenMail 试题
项目 对Spring Boot 2 ,JDK8 +,JUnit5,TestNG,SQL(HSQLDB,MariaDB,PostgreSQL),NoSQL(Redis,MongoDB),Docker,ELK堆栈等进行了一些实验。它演示了以下用途: Maven (最新版本)和Gradle构建工具 ...
下半年的北大青鸟ACCP5.0 S1考试 笔试: 6套题 机试: NET数据查询和显示 火车票信息查询 轿车信息查询 简易电子邮箱 StudentInfomation