使用spring实现邮件的发送实例(含测试,源码,注释)
程序员文章站
2023-12-20 15:22:10
此篇主要讲的是使用spring配置实现邮件发送,与之前的底层实现简便了不少,只需要几个配置就可以了,那么请往下看:
先写个接口
/**
* @ti...
此篇主要讲的是使用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格式的邮件:
接下来是简单邮件:
接下来的一种是使用模板发送邮件,用户名是动态上去的:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。