服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 使用spring实现邮件的发送实例(含测试,源码,注释)

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

2020-11-02 17:50一步一步完善 Java教程

本篇文章主要介绍了使用spring实现邮件的发送实例,详细的介绍了使用spring配置实现邮件发送,含测试,源码,注释,有兴趣的可以下

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

先写个接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* @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();
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* @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;
 }
 
}

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

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

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

我直接上实现类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @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实现邮件的发送实例(含测试,源码,注释)

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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实现邮件的发送实例(含测试,源码,注释)

接下来是简单邮件:

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/qq_34137397/article/details/72814549

延伸 · 阅读

精彩推荐