使用spring实现邮件的发送实例(含测试,源码,注释)

此篇主要讲的是使用spring配置实现邮件发送,与之前的底层实现简便了不少,只需要几个配置就可以了,那么请往下看:

先写个接口

/**

* @Title: IMailserdService.java

* @Package org.service

* @Description: TODO该方法的主要作用:

* @author A18ccms A18ccms_gmail_com

* @date 2017-5-30 上午10:36:34

* @version V1.0

*/

package org.service;

/**

*

* 项目名称:spring_Schop8

* 类名称:IMailserdService

* 类描述:

* 创建人:Mu Xiongxiong

* 修改备注:

* @version

*

*/

public interface IMailsendService {

/**

*

* @Title: sendMessage

* @Description: 该方法的主要作用:发送邮件

* @param 设定文件

* @return 返回类型:void

* @throws

*/

void sendMessage();

}

然后在写个实现该接口的类:

/**

* @Title: AttMailsendServiceImpl.java

* @Package org.service.impl

* @Description: TODO该方法的主要作用:

* @author A18ccms A18ccms_gmail_com

* @date 2017-5-30 上午11:12:02

* @version V1.0

*/

package org.service.impl;

import java.io.IOException;

import javax.mail.MessagingException;

import javax.mail.internet.MimeMessage;

import org.service.IMailsendService;

import org.springframework.core.io.ClassPathResource;

import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.mail.javamail.MimeMessageHelper;

/**

*

* 项目名称:spring_Schop8

* 类名称:AttMailsendServiceImpl

* 类描述: 使用spring实现对邮件的发送

* 创建人:Mu Xiongxiong

* 修改备注:

* @version

*

*/

public class AttMailsendServiceImpl implements IMailsendService {

private JavaMailSender javaMailSender;

/**(非 Javadoc)

* <p>Title: sendMessage</p>

* <p>Description(描述):发送带附件的邮件 </p>

* @see org.service.IMailsendService#sendMessage()

*/

@Override

public void sendMessage() {

MimeMessage message = javaMailSender.createMimeMessage();

MimeMessageHelper helper;

try {

helper = new MimeMessageHelper(message,true,"utf-8");

helper.setFrom("jerry@mail.com");

helper.setTo("tina@mail.com");

helper.setSubject("带附件的邮件");

//普通格式的

//helper.setText("发送一个附件内容!<a href='http://www.baidu.com'>百度搜索</a>");

//html格式的

helper.setText("发送一个附件内容!<a href='http://www.baidu.com'>百度搜索</a>",true);

//添加附件1

ClassPathResource file1 = new ClassPathResource("/org/doc/doc.txt");

helper.addAttachment(file1.getFilename(),file1.getFile());

//添加附件2

ClassPathResource file2 = new ClassPathResource("/org/doc/text.txt");

helper.addAttachment(file2.getFilename(), file2.getFile());

javaMailSender.send(message);

} catch (MessagingException e) {

// TODO 异常执行块!

e.printStackTrace();

} catch (IOException e) {

// TODO 异常执行块!

e.printStackTrace();

}

}

public JavaMailSender getJavaMailSender() {

return javaMailSender;

}

public void setJavaMailSender(JavaMailSender javaMailSender) {

this.javaMailSender = javaMailSender;

}

}

上面的这个类还可以发送带附件的邮件,里面含两个附件(文件),我弄上来吧:

还有一种是使用模板发送带html格式的邮件:

我直接上实现类:

/**

* @Title: CreateMatterServiceImpl.java

* @Package org.service.impl

* @Description: TODO该方法的主要作用:

* @author A18ccms A18ccms_gmail_com

* @date 2017-5-30 上午11:46:53

* @version V1.0

*/

package org.service.impl;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import javax.mail.MessagingException;

import javax.mail.internet.MimeMessage;

import org.service.IMailsendService;

import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.mail.javamail.MimeMessageHelper;

import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import freemarker.template.Configuration;

import freemarker.template.Template;

import freemarker.template.TemplateException;

/**

*

* 项目名称:spring_Schop8

* 类名称:CreateMatterServiceImpl

* 类描述:

* 创建人:Mu Xiongxiong

* 修改备注:

* @version

*

*/

public class CreateMatterServiceImpl implements IMailsendService {

private JavaMailSender javaMailSender;

private Configuration configuration;

/**(非 Javadoc)

* <p>Title: sendMessage</p>

* <p>Description(描述):使用后模板发送邮件 </p>

* @see org.service.IMailsendService#sendMessage()

*/

@Override

public void sendMessage() {

MimeMessage message = javaMailSender.createMimeMessage();

try {

MimeMessageHelper helper = new MimeMessageHelper(message,true,"UTF-8");

helper.setFrom("jerry@mail.com");

helper.setTo("tina@mail.com");

helper.setSubject("使用模板进行发送邮件");

helper.setText(getText(),true);

//从模板里面读取

javaMailSender.send(message);

} catch (MessagingException e) {

// TODO 异常执行块!

e.printStackTrace();

}

}

//读取模板

private String getText(){

String txt = "";

try {

Template template = configuration.getTemplate("mail.ftl");

//通过map传递动态数据

Map map = new HashMap();

map.put("username","雄雄");

//解析模板文件

txt = FreeMarkerTemplateUtils.processTemplateIntoString(template,map);

} catch (IOException e) {

// TODO 异常执行块!

e.printStackTrace();

} catch (TemplateException e) {

// TODO 异常执行块!

e.printStackTrace();

}

return txt;

}

public JavaMailSender getJavaMailSender() {

return javaMailSender;

}

public void setJavaMailSender(JavaMailSender javaMailSender) {

this.javaMailSender = javaMailSender;

}

public Configuration getConfiguration() {

return configuration;

}

public void setConfiguration(Configuration configuration) {

this.configuration = configuration;

}

}

模板文件如下:

然后在看看spring里面是怎么配置的呢?

<?xml version="1.0" encoding="UTF-8"?>

<beans

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<bean id="mailsender" class="org.springframework.mail.javamail.JavaMailSenderImpl">

<property name="host" value="mail.com"></property>

<property name="port" value="25"></property>

<property name="username" value="jerry"></property>

<property name="password" value="123" ></property>

<property name="protocol" value="smtp"></property>

<property name="defaultEncoding" value="utf-8"></property>

<property name="javaMailProperties">

<props>

<prop key="mail.smtp.auth">true</prop>

</props>

</property>

</bean>

<!-- 配置FreeMarker-->

<bean id="freeMarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">

<!-- 指定模板文件路径 -->

<property name="templateLoaderPath" value="org/doc/"></property>

<!-- 设置freekMarker环境变量 -->

<property name="freemarkerSettings">

<props>

<prop key="default_encoding">UTF-8</prop>

</props>

</property>

</bean>

<!-- 简单邮件 -->

<bean id="simpleMailsendService" class="org.service.impl.SimpleMailsendServiceImpl">

<property name="sender" ref="mailsender"></property>

</bean>

<!-- html和带附件的邮件 -->

<bean id="AttMailsendService" class="org.service.impl.AttMailsendServiceImpl">

<property name="javaMailSender" ref="mailsender"></property>

</bean>

<!-- 使用模板发送邮件-->

<bean id="createMatterService" class="org.service.impl.CreateMatterServiceImpl">

<property name="configuration" ref="freeMarkerConfiguration"></property>

<property name="javaMailSender" ref="mailsender"></property>

</bean>

</beans>

当前时间已经是00点多了,又累又困,我就不详细解释这个applicationContexct.xml里面的内容了,里面有注释,看不懂的可以评论,我第一时间改进!

接着我们测试一下:

TestMail:

package org.test;

import org.junit.Test;

import org.service.IMailsendService;

import org.service.impl.Mail;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMail {

@Test

public void testMail() {

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

//简单邮件

//IMailsendService mailsendService=(IMailsendService) ctx.getBean("simpleMailsendService");

//复杂邮件

//IMailsendService mailsendService=(IMailsendService) ctx.getBean("AttMailsendService");

//使用模板的文件

IMailsendService mailsendService=(IMailsendService) ctx.getBean("createMatterService");

mailsendService.sendMessage();

System.out.println("发送成功!");

}

}

注明一下:简单邮件是直接发的文本内容,复杂邮件是包含html格式和附件的,模板发送是html格式的另一种方式,我们来看看运行的结果:

先看看带附件,还有html格式的邮件:

接下来是简单邮件:

接下来的一种是使用模板发送邮件,用户名是动态上去的:

以上是 使用spring实现邮件的发送实例(含测试,源码,注释) 的全部内容, 来源链接: utcz.com/p/213048.html

回到顶部