使用spring實(shí)現(xiàn)郵件的發(fā)送實(shí)例(含測(cè)試,源碼,注釋)
此篇主要講的是使用spring配置實(shí)現(xiàn)郵件發(fā)送,與之前的底層實(shí)現(xiàn)簡(jiǎn)便了不少,只需要幾個(gè)配置就可以了,那么請(qǐng)往下看:
先寫個(gè)接口
/**
* @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;
/**
*
* 項(xiàng)目名稱:spring_Schop8
* 類名稱:IMailserdService
* 類描述:
* 創(chuàng)建人:Mu Xiongxiong
* 修改備注:
* @version
*
*/
public interface IMailsendService {
/**
*
* @Title: sendMessage
* @Description: 該方法的主要作用:發(fā)送郵件
* @param 設(shè)定文件
* @return 返回類型:void
* @throws
*/
void sendMessage();
}
然后在寫個(gè)實(shí)現(xiàn)該接口的類:
/**
* @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;
/**
*
* 項(xiàng)目名稱:spring_Schop8
* 類名稱:AttMailsendServiceImpl
* 類描述: 使用spring實(shí)現(xiàn)對(duì)郵件的發(fā)送
* 創(chuàng)建人:Mu Xiongxiong
* 修改備注:
* @version
*
*/
public class AttMailsendServiceImpl implements IMailsendService {
private JavaMailSender javaMailSender;
/**(非 Javadoc)
* <p>Title: sendMessage</p>
* <p>Description(描述):發(fā)送帶附件的郵件 </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("發(fā)送一個(gè)附件內(nèi)容!<a );
//html格式的
helper.setText("發(fā)送一個(gè)附件內(nèi)容!<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 異常執(zhí)行塊!
e.printStackTrace();
} catch (IOException e) {
// TODO 異常執(zhí)行塊!
e.printStackTrace();
}
}
public JavaMailSender getJavaMailSender() {
return javaMailSender;
}
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
}
上面的這個(gè)類還可以發(fā)送帶附件的郵件,里面含兩個(gè)附件(文件),我弄上來吧:

還有一種是使用模板發(fā)送帶html格式的郵件:
我直接上實(shí)現(xiàn)類:
/**
* @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;
/**
*
* 項(xiàng)目名稱:spring_Schop8
* 類名稱:CreateMatterServiceImpl
* 類描述:
* 創(chuàng)建人:Mu Xiongxiong
* 修改備注:
* @version
*
*/
public class CreateMatterServiceImpl implements IMailsendService {
private JavaMailSender javaMailSender;
private Configuration configuration;
/**(非 Javadoc)
* <p>Title: sendMessage</p>
* <p>Description(描述):使用后模板發(fā)送郵件 </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("使用模板進(jìn)行發(fā)送郵件");
helper.setText(getText(),true);
//從模板里面讀取
javaMailSender.send(message);
} catch (MessagingException e) {
// TODO 異常執(zhí)行塊!
e.printStackTrace();
}
}
//讀取模板
private String getText(){
String txt = "";
try {
Template template = configuration.getTemplate("mail.ftl");
//通過map傳遞動(dòng)態(tài)數(shù)據(jù)
Map map = new HashMap();
map.put("username","雄雄");
//解析模板文件
txt = FreeMarkerTemplateUtils.processTemplateIntoString(template,map);
} catch (IOException e) {
// TODO 異常執(zhí)行塊!
e.printStackTrace();
} catch (TemplateException e) {
// TODO 異常執(zhí)行塊!
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>
<!-- 設(shè)置freekMarker環(huán)境變量 -->
<property name="freemarkerSettings">
<props>
<prop key="default_encoding">UTF-8</prop>
</props>
</property>
</bean>
<!-- 簡(jiǎn)單郵件 -->
<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>
<!-- 使用模板發(fā)送郵件-->
<bean id="createMatterService" class="org.service.impl.CreateMatterServiceImpl">
<property name="configuration" ref="freeMarkerConfiguration"></property>
<property name="javaMailSender" ref="mailsender"></property>
</bean>
</beans>
當(dāng)前時(shí)間已經(jīng)是00點(diǎn)多了,又累又困,我就不詳細(xì)解釋這個(gè)applicationContexct.xml里面的內(nèi)容了,里面有注釋,看不懂的可以評(píng)論,我第一時(shí)間改進(jìn)!
接著我們測(cè)試一下:
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");
//簡(jiǎn)單郵件
//IMailsendService mailsendService=(IMailsendService) ctx.getBean("simpleMailsendService");
//復(fù)雜郵件
//IMailsendService mailsendService=(IMailsendService) ctx.getBean("AttMailsendService");
//使用模板的文件
IMailsendService mailsendService=(IMailsendService) ctx.getBean("createMatterService");
mailsendService.sendMessage();
System.out.println("發(fā)送成功!");
}
}
注明一下:簡(jiǎn)單郵件是直接發(fā)的文本內(nèi)容,復(fù)雜郵件是包含html格式和附件的,模板發(fā)送是html格式的另一種方式,我們來看看運(yùn)行的結(jié)果:
先看看帶附件,還有html格式的郵件:

接下來是簡(jiǎn)單郵件:

接下來的一種是使用模板發(fā)送郵件,用戶名是動(dòng)態(tài)上去的:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring學(xué)習(xí)筆記3之消息隊(duì)列(rabbitmq)發(fā)送郵件功能
- Java的Spring框架中實(shí)現(xiàn)發(fā)送郵件功能的核心代碼示例
- springMVC發(fā)送郵件的簡(jiǎn)單實(shí)現(xiàn)
- Spring Boot實(shí)戰(zhàn)之發(fā)送郵件示例代碼
- Spring Boot中利用JavaMailSender發(fā)送郵件的方法示例(附源碼)
- spring mail借助qq郵箱服務(wù)器發(fā)送郵件
- Spring+quartz實(shí)現(xiàn)定時(shí)發(fā)送郵件功能實(shí)例
- Spring Boot實(shí)現(xiàn)郵件發(fā)送功能
- Spring實(shí)現(xiàn)郵件發(fā)送功能
- spring+maven實(shí)現(xiàn)郵件發(fā)送
相關(guān)文章
java利用JAXB實(shí)現(xiàn)對(duì)象和xml互相轉(zhuǎn)換方法與實(shí)例詳解
這篇文章主要介紹了java利用JAXB實(shí)現(xiàn)對(duì)象和xml互相轉(zhuǎn)換方法與實(shí)例詳解,需要的朋友可以參考下2020-02-02
Java中private關(guān)鍵字詳細(xì)用法實(shí)例以及解釋
這篇文章主要給大家介紹了關(guān)于Java中private關(guān)鍵字詳細(xì)用法實(shí)例以及解釋的相關(guān)資料,在Java中private是一種訪問修飾符,它可以用來控制類成員的訪問權(quán)限,文中將用法介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
SpringBoot使用validation-api實(shí)現(xiàn)對(duì)枚舉類參數(shù)校驗(yàn)的方法
這篇文章主要介紹了SpringBoot使用validation-api實(shí)現(xiàn)對(duì)枚舉類參數(shù)校驗(yàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Maven中兩個(gè)命令clean 和 install的使用
Maven是一個(gè)項(xiàng)目管理和自動(dòng)構(gòu)建工具,clean命令用于刪除項(xiàng)目中由先前構(gòu)建生成的target目錄,install命令用于將打包好的jar包安裝到本地倉(cāng)庫(kù)中,供其他項(xiàng)目依賴使用,下面就來詳細(xì)的介紹一下這兩個(gè)命令2024-09-09
基于Java8并行流(parallelStream)的注意點(diǎn)
這篇文章主要介紹了Java8并行流(parallelStream)的注意點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java實(shí)現(xiàn)銀行ATM系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)銀行ATM系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
springboot 整合fluent mybatis的過程,看這篇夠了
這篇文章主要介紹了springboot 整合fluent mybatis的過程,配置數(shù)據(jù)庫(kù)連接創(chuàng)建數(shù)據(jù)庫(kù)的詳細(xì)代碼,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08

