Spring与JavaMail
JavaMail与Spring集成开发
spring框架集成JavaMail的主要包
2.mail.properties
mail.smtp.host=smtp.163.commail.smtp.auth=truemail.username=15511111111mail.password=123mail.from=15511111111@163.com
3.使用spring配置(applicationContext-mail.xml)
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><description>JavaMail配置文件</description><!--加载mail.properties文件--><context:property-placeholderlocation="classpath:mail.properties"/><!--配置一个简单邮件对象--><beanid="mailMessage"class="org.springframework.mail.SimpleMailMessage"><propertyname="from"value="${mail.from}"></property></bean><!--邮件的发送对象--><beanid="mailSender"class="org.springframework.mail.javamail.JavaMailSenderImpl"><propertyname="host"value="${mail.smtp.host}"></property><propertyname="username"value="${mail.username}"></property><propertyname="password"value="${mail.password}"></property><propertyname="defaultEncoding"value="UTF-8"></property><!--邮件发送相关的配置信息--><propertyname="javaMailProperties"><props><propkey="mail.smtp.auth">${mail.smtp.auth}</prop><propkey="mail.debug">true</prop><propkey="mail.smtp.timeout">0</prop></props></property></bean></beans>
4.发送简单邮件代码
publicvoidtestJavaMail()throwsException{ApplicationContextac=newClassPathXmlApplicationContext("classpath:applicationContext-mail.xml");SimpleMailMessagemessage=(SimpleMailMessage)ac.getBean("mailMessage");//加载简单邮件对象JavaMailSendersender=(JavaMailSender)ac.getBean("mailSender");//得到邮件的发送对象,专门用于邮件发送//设置简单邮件对象的属性message.setSubject("spring与javamail的测试");//主题message.setText("hello,springandjavamail");//内容message.setTo("174111111@qq.com");//收件箱//发送邮件sender.send(message);}
5.发送带有图片和带附件的邮件
publicvoidtestJavaMail()throwsException{ApplicationContextac=newClassPathXmlApplicationContext("classpath:applicationContext-mail.xml");JavaMailSendersender=(JavaMailSender)ac.getBean("mailSender");//得到邮件的发送对象,专门用于邮件发送//发送一个允许带图片,同时带附件的邮件MimeMessagemessage=sender.createMimeMessage();//创建一封允许带图片,同时带附件的邮件对象//为了更好的操作MimeMessage对象,借用一个工具类来操作它MimeMessageHelperhelper=newMimeMessageHelper(message,true);//通过工具类设置主题,内容,图片,附件helper.setFrom("155111111111@163.com");helper.setTo("17411111@qq.com");helper.setSubject("这是来自x网的一个请求");helper.setText("<html><head></head><body><h2>hello!!baby</h2>"+"<ahref=http://www.baidu.com>去百度</a>"+"<imgsrc=cid:p_w_picpath/></body></html>",true);//第二个参数说明内容要解析为html代码//添加图片FileSystemResourceresource=newFileSystemResource(newFile("E:\\原理分析.png"));helper.addInline("p_w_picpath",resource);sender.send(message);/*JavaMailSenderImplmailSender=(JavaMailSenderImpl)ac.getBean("mailSender");//3.创建一封允许带附件的邮件对象MimeMessagemimeMessage=mailSender.createMimeMessage();//创建出允许带附件的邮件对象//4.创建出一个用于操作MimeMessage的帮助类的对象MimeMessageHelperhelper=newMimeMessageHelper(mimeMessage,true);//5.设置邮件的相关内容(发送者,拼接者,主题,内容)helper.setFrom("15511111@163.com");helper.setTo("174@qq.com");helper.setSubject("带图片和附件的邮件测试");helper.setText("<html><head></head><body><h2>hello!!springp_w_picpathhtmlmail</h2>"+"<ahref=http://www.itheima.com>去百度</a>"+"<imgsrc=cid:p_w_picpath/></body></html>",true);//cid:是固定的,后面的p_w_picpath是自己定义的//指定p_w_picpath所在的位置(是指本地电脑)FileSystemResourceimg=newFileSystemResource(newFile("E:/原理分析.png"));//将本地的图片转化成一个图片资源helper.addInline("p_w_picpath",img);//p_w_picpath的参数来自上面cid的取值//发送时带附件FileSystemResourcezipResource=newFileSystemResource(newFile("E:/javamail1_4_4.zip"));helper.addAttachment("javamail1_4_4.zip",zipResource);*///发送邮件//mailSender.send(mimeMessage);}
项目中使用spring中的JavaMail工具类
01.需要在类中注入相关对象
privateSimpleMailMessagemailMessage;privateJavaMailSendermailSender;publicvoidsetMailMessage(SimpleMailMessagemailMessage){this.mailMessage=mailMessage;}publicvoidsetMailSender(JavaMailSendermailSender){this.mailSender=mailSender;}
02.在spring配置文件中配置
<beanid="userService"class="com.my.qb.service.impl.UserServiceImpl"><propertyname="baseDao"ref="baseDao"></property><propertyname="mailMessage"ref="mailMessage"></property><propertyname="mailSender"ref="mailSender"></property></bean>
03.实现功能(比如:在注册的时候,发送邮件,邮件可能发送不成功,但要保证注册完成)
/***新增用户*/publicvoidsaveOrUpdate(finalUserentity){if(UtilFuns.isEmpty(entity.getId())){//新增Stringid=UUID.randomUUID().toString();entity.setId(id);entity.getUserinfo().setId(id);//补充Shiro添加后的bugentity.setPassword(Encrypt.md5(SysConstant.DEFAULT_PASS,entity.getUserName()));baseDao.saveOrUpdate(entity);//记录保存//独立的比较费时的,都交给线程来做//spring集成javaMailThreadth=newThread(newRunnable(){publicvoidrun(){try{mailMessage.setTo(entity.getUserinfo().getEmail());mailMessage.setSubject("新员工入职的系统账户通知");mailMessage.setText("欢迎您加入本集团,您的用户名:"+entity.getUserName()+",初始密码:"+SysConstant.DEFAULT_PASS);mailSender.send(mailMessage);}catch(Exceptione){e.printStackTrace();}}});th.start();}else{//修改baseDao.saveOrUpdate(entity);}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。