Maven+Spring实现邮件发送

来源:互联网 发布:淘宝优质网店怎么搜 编辑:程序博客网 时间:2024/06/06 21:00

Maven+Spring实现邮件发送

本例使用Maven以及Spring实现简单的邮件发送,旨在记录自己的学习过程以及此过程中遇到的问题和解决方法。

  • 项目POM文件
  • Java主代码
  • xml配置&properties文件
  • 测试代码
  • 个别问题说明

项目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.wh.learn.mvn</groupId>  <artifactId>accountemail</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>jar</packaging>  <name>accountemail</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.7</version>      <scope>test</scope>    </dependency>    <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>  </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>

Java主代码

package com.wh.learn.mvn.accountemail;public interface AccountEmailService {    void sendMail(String to, String subject, String htmlText) throws AccountEmailException;}
package com.wh.learn.mvn.accountemail;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 {        MimeMessage msg = javaMailSender.createMimeMessage();        MimeMessageHelper msgHelper = new MimeMessageHelper(msg);        try {            msgHelper.setFrom(systemEmail);            msgHelper.setTo(to);            msgHelper.setSubject(subject);            msgHelper.setText(htmlText, true);            javaMailSender.send(msg);        } catch (MessagingException e) {            throw new AccountEmailException("Faild to send mail!",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;    }}

xml配置&properties文件

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.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.protocol}</prop>            </props>        </property>    </bean>     <bean id="accountEmailService"    class="com.wh.learn.mvn.accountemail.AccountEmailServiceImpl">        <property name="javaMailSender" ref="javaMailSender"/>        <property name="systemEmail" value="${email.systemEmail}"/>    </bean> </beans>

properties文件

email.protocol=smtpsemail.host=smtp.163.comemail.port=465email.username=your-username@163.comemail.password=your-passwordemail.auth=trueemail.systemEmail=your-username@163.com

测试代码

package com.wh.learn.mvn.accountemail;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.ServerSetup;public class TestAccountEmailService {    private GreenMail greenMail;    @Before    public void startMailServer() throws Exception{        greenMail = new GreenMail(ServerSetup.SMTP);        greenMail.setUser("your-username@163.cm","yourpasswordd");        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("your-receive-mail@qq.com", subject, htmlText);    }    @After    public void stopMailServer() throws Exception{        greenMail.stop();    }}
1 0
原创粉丝点击