1. greenmail introduction greenmail is an open-source, intuitive and easy-to-use mail server test suite for testing. It supports SMTP, POP3, and IMAP. It also provides greenmail services for JBoss. Greenmail is the first and only library that provides a test framework for receiving and retrieving emails from Java. Home page: http://www.icegreen.com/greenmail /. 2. greenmail in actual project use (1) Project pom file: <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> J Ar </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> <groupid> Org. springframework </Groupid> <artifactid> spring-beans </artifactid> <version >$ {spring. version }</version> </dependency> <groupid> Org. springframework </groupid> <artifactid> spring-context </artifactid> <version >$ {spring. version }</version> </dependency> <groupid> Org. springframework </groupid> <artifactid> spring-context-support </artifactid> <version >$ {spring. version }</version> </dependency> <groupid> Javax. mail </groupid> <artifactid> mail </artifactid> <version> 1.5.0-B01 </version> </dependency> <groupid> JUnit </groupid> <artifactid> JUnit </artifactid> <version> 4.11 </version> <scope> test </scope> </dependency> <groupid> COM. icegreen </groupid> <artifactid> greenmail </artifactid> <version> 1.3.1b </version> <scope> test </scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupid> Org. AP Ache. maven. plugins </groupid> <artifactid> Maven-compiler-plugin </artifactid> <configuration> <source> 1.5 </source> <target> 1.5 </Target> </configuration> </plugin> </plugins> </build> </Project> (2) email sending interface:/** copyright 2013-2015 */package COM. jackie. codeproject. account. email;/*** title: accountemailservice. java * Email interface ** @ author Jackie * @ since Mar 30,201 3 5:58:07 * @ version V1.0 */public interface Accountemailservice {/*** method of sending an email * @ author Jackie * @ date Mar 30,201 3 * @ Param to * @ Param subject * @ Param htmltext * @ throws exception * @ return void * /void sendemail (string, string subject, string htmltext) throws accountemailexception;} (2) mail sending interface implementation class:/** copyright 2013-2015 */package COM. jackie. codeproject. account. email; import javax. mail. messagingexception; import javax. mail. internet. m Imemessage; import Org. springframework. mail. javamail. javamailsender; import Org. springframework. mail. javamail. mimemessagehelper;/*** title: accountemailserviceimpl. java * mail implementation class ** @ author Jackie * @ since Mar 30,201 3 6:11:40 * @ version V1.0 */public class accountemailserviceimpl implements accountemailservice {/*** @ fields javamailsender: tool class for simplified email sending */private javamailsender javamailsende R;/*** @ fields systememail: System email account */private string systememail; Public void sendemail (string to, string subject, string htmltext) throws accountemailexception {try {// use javamailsender to create a mimemessage MSG, corresponding to the mail to be sent mimemessage MSG = javamailsender. createmimemessage (); // create a mimemessagehelper msghelper new mimemessagehelper (MSG); // use mimemessagehel Per sets the mail sending Address, recipient address, subject, and content msghelper. setfrom (systememail); msghelper. setto (to); msghelper. setsubject (subject); msghelper. settext (htmltext, true); // send the javamailsender email. send (MSG);} catch (messagingexception e) {Throw new accountemailexception ("failed to send email. ", e) ;}}/*** @ return the javamailsender */Public javamailsender getjavamailsender () {return javamailsender;}/*** @ Param javama Ilsender * The javamailsender to set */Public void setjavamailsender (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) custom exception class:/** copyright 2013-201 5 */package COM. jackie. codeproject. account. email;/*** title: accountemailexception. java Custom Email exception ** @ author Jackie * @ since Mar 30,201 3 6:01:31 * @ version V1.0 */public class accountemailexception extends exception {/*** @ fields serialversionuid */private static final long serialversionuid =-5399025975888079256l; /*** error message */private string message; Public accountemailexception (string me Ssage) {This. message = message;} public accountemailexception (string message, 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 profile 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. propertyplaceholderpolicer "> <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 of the attribute file. properties: # protocol email. protocol = SMTP # server email. host = localhost # port email. port = 25 # username email. username = test@gmail.com # password email. password = 123456 # The server requires the user name and password for authentication email. auth = true # system email. systememail = jackie@sina.com (7) Test class:/** 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 mail service test class ** @ author Jackie * @ since Mar 30,201 3 6:13:14 * @ version V1.0 */public class accountemailservicetest {private greenmail; private applicationcontext; /*** start the email server ** @ author Jackie * @ date Mar 30,201 3 * @ 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) applicationcontext. getbean ("accountemailservice"); string subject = "test subject"; string htmltext = "