`

Maven account-email例子

 
阅读更多

1.在Eclipse中新建Maven工程。

2.修改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.iteye.xujava</groupId>
  <artifactId>account-email</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <name>account-email</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-core</artifactId>
  		<version>2.5.6</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-beans</artifactId>
  		<version>2.5.6</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-context</artifactId>
  		<version>2.5.6</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-context-support</artifactId>
  		<version>2.5.6</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.9</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

 

2.在src/main/java的包com.iteye.xujava.account.email下新建三个文件AccountEmailException、AccountEmailService、AccountEmailServiceImpl

package com.iteye.xujava.account.email;

public class AccountEmailException extends Exception {

	private static final long serialVersionUID = 6514881539290222459L;

	public AccountEmailException(String message) {
		super(message);
	}

	public AccountEmailException(String message, Throwable throwable) {
		super(message, throwable);
	}

}

 

package com.iteye.xujava.account.email;

public interface AccountEmailService {

	void sendMail(String to,String subject,String htmlText) throws AccountEmailException;
}

 

package com.iteye.xujava.account.email;

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 AccountEmailException {
		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);
			System.out.println("发送完毕");
		} catch (MessagingException e) {
			throw new AccountEmailException("发送邮件失败...", 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;
	}

}

 

3.在src/main/resources目录下新建account-email.xml和service.properties文件

<?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.5.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.iteye.xujava.account.email.AccountEmailServiceImpl">
		<property name="javaMailSender" ref="javaMailSender" />
		<property name="systemEmail" value="${email.systemEmail}" />
	</bean>
</beans>

 

#真实
#email.protocol=smtp
#email.host=smtp.163.com
#email.port=25
#email.username=test@163.com
#email.password=password
#email.auth=true
#email.systemEmail=test@163.com

#测试
email.protocol=smtp
email.host=127.0.0.1
email.port=25
email.username=test@xujava.iteye.com
email.password=123456
email.auth=true
email.systemEmail=test@xujava.iteye.com

 

4.在src/test/java的包 com.iteye.xujava.account.email下新建测试文件

package com.iteye.xujava.account.email;

import static org.junit.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;

public class AccountEmailServiceTest {
	private GreenMail greenMail;

	@Before
	public void startMailServer() throws Exception {
		greenMail = new GreenMail(ServerSetup.SMTP);
		greenMail.setUser("test@xujava.iteye.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 = "Text Subject";
		String htmlText = "<h3>Test</h3>";

		accountEmailService.sendMail("test2@xujava.iteye.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());
	}

	@After
	public void stopMainServer() throws Exception {
		greenMail.stop();
	}
}

 

5.在命令提示行找到account-email目录,输入mvn clean test测试,成功后,输入mvn clean install安装到Maven库。

 

 

 

 

 

 

分享到:
评论

相关推荐

    Maven account-web例子

    7. Maven插件:Maven可以通过插件扩展功能,例如maven-war-plugin用于打包Web应用,maven-surefire-plugin用于执行测试。 通过博文链接(https://xujava.iteye.com/blog/1888949)可以获取更详细的步骤和代码示例,...

    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-...

    Maven exec-maven-plugin:执行外部命令的实践指南

    exec-maven-plugin是Maven生态系统中的一个插件,它允许用户在Maven构建过程中执行外部命令或脚本。这使得Maven项目可以集成更多的自定义操作,比如运行特定的脚本、调用系统命令等。本文将详细介绍exec-maven-...

    apache-maven-3.8.6-bin.tar.tz--test

    apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-...

    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-...

    eclipse-maven3-plugin

    **eclipse-maven3-plugin** 是一个专门为 Eclipse IDE 设计的插件,它允许开发者在 Eclipse 开发环境中无缝集成 Maven 构建工具。Maven 是一个项目管理和综合工具,广泛用于 Java 应用程序的构建、依赖管理和项目...

    maven3-plugin-3.0.1-sources.jar

    maven3-plugin-3.0.1-sources.jar

    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全版本资源,Maven 3.0.5-3.8.5,每个版本包含4个文件,Maven3全资源打包下载,Maven全集

    apache-maven-3.0.5 apache-maven-3.1.1 apache-maven-3.2.5 apache-maven-3.3.9 apache-maven-3.5.4 apache-maven-3.6.3 apache-maven-3.8.5 每个版本包含4个文件: apache-maven-3.8.5-bin.tar.gz apache-maven-...

    eclipse-maven3-plugin Maven插件离线安装包

    打开并输入:path= D:/Development/eclipse-JavaEE/eclipse/plugins/maven(请参照上面对应你的 maven 插件) 4. 重启 eclipse,OK,完成了,启动后你打开Window ---&gt; Preferences 会发现一个多了一个选项Maven...

    maven-compiler-plugin-3.8.0-source-release插件

    `maven-compiler-plugin-3.8.0-source-release` 是 Maven 生态系统中不可或缺的一部分,它提供了可靠的源代码编译功能,使得开发者能够专注于编写代码,而无需关心构建过程的细节。通过理解 Maven 插件的工作原理和...

    maven-deploy-plugin-2.8.2.jar

    maven-deploy-plugin-2.8.2.jar

    下载慢?给你apache maven 3.x.x所有Linux, Windows版本下载的百度网盘链接

    apache-maven-3.0.4-bin.tar.gz apache-maven-3.0.4-bin.zip apache-maven-3.0.5-bin.tar.gz apache-maven-3.0.5-bin.zip apache-maven-3.1.0-bin.tar.gz apache-maven-3.1.0-bin.zip apache-maven-3.1.1-bin.tar.gz...

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

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

    maven-jar-plugin-3.1.1.jar

    maven-jar-plugin-3.1.1.jar

    maven3-plugin-3.0.0-1-sources.jar

    maven3-plugin-3.0.0-1-sources.jar

    maven3.8.1-windows-x64.rar

    - 解压后,你会得到一个名为“apache-maven-3.8.1”的文件夹,这个就是Maven的安装目录。 **3. 设置环境变量** 为了使Maven在命令行中可直接使用,需要设置两个环境变量: - `M2_HOME`:指向Maven的安装目录,例如...

    apache-maven-3.9.0-bin.tar

    "apache-maven-3.9.0-bin.tar" 是Apache Maven 3.9.0版本的Linux二进制发行版,以tar归档格式提供。这个版本包含了运行Maven所需的所有文件,包括可执行脚本、库文件和文档。用户在Linux环境下,可以将此文件移动到...

    maven-antrun-plugin-3.0.0.jar

    maven-antrun-plugin-3.0.0.jar

Global site tag (gtag.js) - Google Analytics