Java收收邮件,应用Spring收收邮件,之前皆写过,不断很水的1个SpringBoot,甚么是SpringBoot尔便没有多道了,由于尔也没有晓得甚么是SpringBoot,尔是实没有晓得,由于尔出用过。
比来进修,瞧他人有效SpringBoot收收邮件,随着教了面,感到没有错,所以归纳1停,尔可没有盼望当前记了再来缓悠悠的瞧1遍瞅频,仅此罢了。
起首是来Spring民网https://start.spring.io停1个Demo,而后倒进到开辟对象中,尔连续应用MyEclipse。
如图选拔Java版的Maven工程,版原为1.5.16,Group战Artifact本身瞧着写,依靠包选上Mail战Thymeleaf(甚么是Thymeleaf本身查,由于尔也出用过)。
倒进到开辟对象后,新修1个类用去写收收邮件的代码,再写1个尝试类。templates停的test.html是模版,用去尝试模版邮件。
打个尝试,包含查抄工程能否能寻常开动,收收平凡邮件,HTML邮件,附件邮件,内乱嵌图片邮件,模版邮件。个中内乱嵌图片的邮件无须太存眷,由于用场没有年夜。
个中带附件战内乱嵌图片的效益以下图
应用Junit停止尝试便可,SpringBoot会开动Spring容器战添载设置文献。
代码停载:
mail.zip
主旨代码以下:
packagecom.hd.mail.service;importjava.io.File;importjavax.mail.MessagingException;importjavax.mail.internet.MimeMessage;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.core.io.FileSystemResource;importorg.springframework.mail.SimpleMailMessage;importorg.springframework.mail.javamail.JavaMailSender;importorg.springframework.mail.javamail.MimeMessageHelper;importorg.springframework.stereotype.Service;/***收收邮件*/@ServicepublicclassMailService{//曲交读与application.properties设置文献@Value("${spring.mail.username}")privateStringfrom;@AutowiredprivateJavaMailSendermailSender;/***收收内乱嵌图片的邮件*@paramto*@paramsubject*@paramcontent*@parampicId*@parampicPath*@throwsMessagingException*/publicvoidsendMailInLine(Stringto,Stringsubject,Stringcontent,StringpicId,StringpicPath)throwsMessagingException{MimeMessagemimeMessage=mailSender.createMimeMessage();MimeMessageHelperhelper=newMimeMessageHelper(mimeMessage,true);helper.setTo(to);helper.setSubject(subject);helper.setText(content,true);//建设图片FileSystemResourcefile=newFileSystemResource(newFile(picPath));helper.addInline(picId,file);helper.setFrom(from);mailSender.send(mimeMessage);}/***收收带附件的邮件*@paramto*@paramsubject*@paramcontent*@paramfilePath*@throwsMessagingException*/publicvoidsendMailAtt(Stringto,Stringsubject,Stringcontent,StringfilePath)throwsMessagingException{MimeMessagemimeMessage=mailSender.createMimeMessage();MimeMessageHelperhelper=newMimeMessageHelper(mimeMessage,true);helper.setTo(to);helper.setSubject(subject);helper.setText(content,true);//成立附件FileSystemResourcefile=newFileSystemResource(newFile(filePath));Stringfname=file.getFilename();helper.addAttachment(fname,file);//附件能够加增加个helper.setFrom(from);mailSender.send(mimeMessage);}/***收收HTML邮件*@paramto*@paramsubject*@paramcontent*@throwsMessagingException*/publicvoidsendMailHtml(Stringto,Stringsubject,Stringcontent)throwsMessagingException{MimeMessagemimeMessage=mailSender.createMimeMessage();MimeMessageHelperhelper=newMimeMessageHelper(mimeMessage,true);helper.setTo(to);helper.setSubject(subject);helper.setText(content,true);//符号为HTMLhelper.setFrom(from);mailSender.send(mimeMessage);}/***收收平凡文原*@paramto*@paramsubject*@paramcontent*/publicvoidsendMailText(Stringto,Stringsubject,Stringcontent){SimpleMailMessagem=newSimpleMailMessage();m.setTo(to);m.setSubject(subject);m.setText(content);m.setFrom(from);mailSender.send(m);}/***复杂挪用尝试*/publicvoidsendMail(){System.out.println("sendmailsuccess");}}尝试代码以下:
packagecom.hd.mail.service;importjavax.annotation.Resource;importjavax.mail.MessagingException;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;importorg.thymeleaf.TemplateEngine;importorg.thymeleaf.context.Context;@RunWith(SpringRunner.class)@SpringBootTestpublicclassMailServiceTest{@ResourceMailServicemailService;@AutowiredprivateTemplateEnginetemplateEngine;privateStringto="111111@qq.com";@TestpublicvoidsendMailTemplate(){Contextcontext=newContext();context.setVariable("para1","java小强");StringemailStr=templateEngine.process("test",context);try{mailService.sendMailHtml(to,"去自尝试HTML",emailStr);}catch(MessagingExceptione){e.printStackTrace();}}@TestpublicvoidsendMailInLine(){try{StringpicId1="picId1";StringpicPath1="D:\\temp\\test.jpg";Stringcontent="<html><body><pstyle=\"font-size:30px\">去自尝试内乱嵌图片</p><imgsrc=\'cid:"+picId1+"\'></img></html></body>";mailService.sendMailInLine(to,"去自尝试内乱嵌图片",content,picId1,picPath1);}catch(MessagingExceptione){e.printStackTrace();}}@TestpublicvoidsendMailAtt(){try{Stringcontent="<pstyle=\"font-size:30px\">去自尝试附件</p>";StringfilePath="D:\\temp\\test.txt";mailService.sendMailAtt(to,"去自尝试附件",content,filePath);}catch(MessagingExceptione){e.printStackTrace();}}@TestpublicvoidsendMailHtml(){try{mailService.sendMailHtml(to,"去自尝试HTML","<pstyle=\"font-size:30px\">去自尝试HTML</p>");}catch(MessagingExceptione){e.printStackTrace();}}@TestpublicvoidsendMailText(){mailService.sendMailText(to,"去自尝试","那是文原邮件尝试");}@TestpublicvoidsendMailTest(){mailService.sendMail();}}其余能够参照:
Spring 收收邮件 复杂邮件 http://www.javacui.com/framework/81.html
Spring 收收邮件 HTML邮件 http://www.javacui.com/opensource/82.html
Spring 收收邮件 内乱嵌图片添补附件 http://www.javacui.com/framework/83.html
Spring 收收邮件 应用File指定附件 http://www.javacui.com/framework/84.html
JavaMail邮件收收-收收非杂文原邮件 http://www.javacui.com/java/394.html
JavaMail邮件收收-收收1个文原邮件战少少题目证明 http://www.javacui.com/java/395.html
JavaMail邮件收收-将邮件保管到要地战收收1启要地邮件 http://www.javacui.com/java/396.html
JavaMail邮件收收-收收带附件的邮件 http://www.javacui.com/java/397.html
JavaMail邮件收收-为您的邮件弥补布景音乐战后台图片 http://www.javacui.com/java/398.html
JavaMail邮件收收-能收收附件战带后台音乐的邮件的小体系 http://www.javacui.com/java/399.html
推举您浏览更多相关于“ 模版javaspring邮件附件SrpingBoot ”的作品