您好,登錄后才能下訂單哦!
JavaMail與Spring集成開發
spring框架集成JavaMail的主要包
2.mail.properties
mail.smtp.host=smtp.163.com mail.smtp.auth=true mail.username=15511111111 mail.password=123 mail.from=15511111111@163.com
3.使用spring配置(applicationContext-mail.xml)
<?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" 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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <description>JavaMail配置文件</description> <!-- 加載mail.properties文件 --> <context:property-placeholder location="classpath:mail.properties"/> <!-- 配置一個簡單郵件對象 --> <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="${mail.from}"></property> </bean> <!-- 郵件的發送對象 --> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="${mail.smtp.host}"></property> <property name="username" value="${mail.username}"></property> <property name="password" value="${mail.password}"></property> <property name="defaultEncoding" value="UTF-8"></property> <!-- 郵件發送相關的配置信息 --> <property name="javaMailProperties" > <props> <prop key="mail.smtp.auth">${mail.smtp.auth}</prop> <prop key="mail.debug">true</prop> <prop key="mail.smtp.timeout">0</prop> </props> </property> </bean> </beans>
4.發送簡單郵件代碼
public void testJavaMail() throws Exception{ ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-mail.xml"); SimpleMailMessage message = (SimpleMailMessage) ac.getBean("mailMessage");//加載簡單郵件對象 JavaMailSender sender = (JavaMailSender) ac.getBean("mailSender"); //得到郵件的發送對象,專門用于郵件發送 //設置簡單郵件對象的屬性 message.setSubject("spring與javamail的測試");//主題 message.setText("hello,spring and javamail ");//內容 message.setTo("174111111@qq.com");//收件箱 //發送郵件 sender.send(message); }
5.發送帶有圖片和帶附件的郵件
public void testJavaMail() throws Exception{ ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-mail.xml"); JavaMailSender sender = (JavaMailSender) ac.getBean("mailSender"); //得到郵件的發送對象,專門用于郵件發送 //發送一個允許帶圖片,同時帶附件的郵件 MimeMessage message = sender.createMimeMessage();//創建一封允許帶圖片,同時帶附件的郵件對象 //為了更好的操作MimeMessage對象,借用一個工具類來操作它 MimeMessageHelper helper = new MimeMessageHelper(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>" +"<a href=http://www.baidu.com>去百度</a>" + "<img src=cid:p_w_picpath/></body></html>",true);//第二個參數說明內容要解析為html代碼 //添加圖片 FileSystemResource resource = new FileSystemResource(new File("E:\\原理分析.png")); helper.addInline("p_w_picpath", resource); sender.send(message); /*JavaMailSenderImpl mailSender = (JavaMailSenderImpl) ac.getBean("mailSender"); //3.創建一封允許帶附件的郵件對象 MimeMessage mimeMessage = mailSender.createMimeMessage();//創建出允許帶附件的郵件對象 //4.創建出一個用于操作MimeMessage的幫助類的對象 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); //5.設置郵件的相關內容 (發送者,拼接者,主題,內容 ) helper.setFrom("15511111@163.com"); helper.setTo("174@qq.com"); helper.setSubject("帶圖片和附件的郵件測試"); helper.setText("<html><head></head><body><h2>hello!!spring p_w_picpath html mail</h2>" +"<a href=http://www.itheima.com>去百度</a>" + "<img src=cid:p_w_picpath/></body></html>", true);//cid:是固定的,后面的p_w_picpath是自己定義的 //指定p_w_picpath所在的位置(是指本地電腦) FileSystemResource img = new FileSystemResource(new File("E:/原理分析.png"));//將本地的圖片轉化成一個圖片資源 helper.addInline("p_w_picpath", img);//p_w_picpath的參數來自上面cid的取值 //發送時帶附件 FileSystemResource zipResource = new FileSystemResource(new File("E:/javamail1_4_4.zip")); helper.addAttachment("javamail1_4_4.zip", zipResource);*/ //發送郵件 //mailSender.send(mimeMessage); }
項目中使用spring中的JavaMail工具類
01.需要在類中注入相關對象
private SimpleMailMessage mailMessage; private JavaMailSender mailSender; public void setMailMessage(SimpleMailMessage mailMessage) { this.mailMessage = mailMessage; } public void setMailSender(JavaMailSender mailSender) { this.mailSender = mailSender; }
02.在spring配置文件中配置
<bean id="userService" class="com.my.qb.service.impl.UserServiceImpl"> <property name="baseDao" ref="baseDao"></property> <property name="mailMessage" ref="mailMessage"></property> <property name="mailSender" ref="mailSender"></property> </bean>
03.實現功能(比如:在注冊的時候,發送郵件,郵件可能發送不成功,但要保證注冊完成)
/** * 新增用戶 */ public void saveOrUpdate(final User entity) { if(UtilFuns.isEmpty(entity.getId())){ //新增 String id = UUID.randomUUID().toString(); entity.setId(id); entity.getUserinfo().setId(id); //補充Shiro添加后的bug entity.setPassword(Encrypt.md5(SysConstant.DEFAULT_PASS, entity.getUserName())); baseDao.saveOrUpdate(entity);//記錄保存 //獨立的比較費時的,都交給線程來做 //spring集成javaMail Thread th = new Thread(new Runnable() { public void run() { try { mailMessage.setTo(entity.getUserinfo().getEmail()); mailMessage.setSubject("新員工入職的系統賬戶通知"); mailMessage.setText("歡迎您加入本集團,您的用戶名:"+entity.getUserName()+",初始密碼:"+SysConstant.DEFAULT_PASS); mailSender.send(mailMessage); } catch (Exception e) { e.printStackTrace(); } } }); th.start(); }else{ //修改 baseDao.saveOrUpdate(entity); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。