springBoot后台发送内容至邮箱
生活随笔
收集整理的這篇文章主要介紹了
springBoot后台发送内容至邮箱
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
發送郵箱
第一步需要去申請一個郵箱,我這里用了網易云郵箱
郵箱必須開啟服務
添加配置
email:host: smtp.163.comport: 25username: ***@163.compassword: ******timeout: 25000添加配置類
package com.hbyh.bam.test;import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;import java.io.*; import java.util.*;@Component @ConfigurationProperties(prefix="email") //接收application.yml中的system下面的屬性 public class MailConfig {@Value("${host}")public String host;@Value("${port}")public String port;@Value("${email.username}")public String userName;@Value("${password}")public String passWord;@Value("${email.username}")public String emailForm;@Value("${timeout}")public String timeout;public String personal="***系統";public String subject="標題";public Properties properties = new Properties(); }添加工具類
package com.hbyh.bam.test;import java.io.*; import java.util.*; import javax.mail.internet.*;import com.hbyh.bam.utils.SpringUtil; import org.springframework.mail.javamail.*; import org.springframework.web.multipart.MultipartFile;public class MailUtil {private MailConfig mc= SpringUtil.getBean(MailConfig.class);private JavaMailSenderImpl mailSender = createMailSender();/*** 郵件發送器** @return 配置好的工具*/private JavaMailSenderImpl createMailSender() {JavaMailSenderImpl sender = new JavaMailSenderImpl();sender.setHost(mc.host);sender.setPort(Integer.parseInt(mc.port));sender.setUsername(mc.userName);sender.setPassword(mc.passWord);sender.setDefaultEncoding("Utf-8");Properties p = new Properties();p.setProperty("mail.smtp.timeout", mc.timeout);p.setProperty("mail.smtp.auth", "false");sender.setJavaMailProperties(p);return sender;}/*** 發送郵件** @param to 接受人* @param html 發送內容*/public int sendMail(String to, String html) {int cg=0;try{MimeMessage mimeMessage = mailSender.createMimeMessage();// 設置utf-8或GBK編碼,否則郵件會有亂碼MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");messageHelper.setFrom(mc.emailForm, mc.personal);messageHelper.setTo(to);messageHelper.setSubject(mc.subject);messageHelper.setText(html, true);mailSender.send(mimeMessage);cg=1;}catch (Exception e){cg=-1;e.printStackTrace();}return cg;}public int sendMail(MultipartFile multipartFile, String to, String html) {int cg=0;try{File file = MultiToFile(multipartFile);MimeMessage mimeMessage = mailSender.createMimeMessage();// 設置utf-8或GBK編碼,否則郵件會有亂碼MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");messageHelper.setFrom(mc.emailForm, mc.personal);messageHelper.setTo(to);messageHelper.setSubject(mc.subject);messageHelper.setText(html!=null?html:"", true);if(file!=null){//獲取文件名稱String filename=file.getName();//添加附件messageHelper.addAttachment(filename, file);//附件}mailSender.send(mimeMessage);cg=1;}catch (Exception e){cg=-1;e.printStackTrace();}return cg;}/*** 將multipartFile文件轉為File文件*/public File MultiToFile(MultipartFile multipartFile) {if(multipartFile==null){return null;}//獲取file名字String filename=multipartFile.getOriginalFilename();File file = new File(filename);try {InputStream ins=multipartFile.getInputStream();OutputStream os = new FileOutputStream(file);int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {os.write(buffer, 0, bytesRead);}os.close();ins.close();} catch (Exception e){file=null;e.printStackTrace();}return file;} }最后調用
package com.hbyh.bam.test;import com.hbyh.bam.dto.RespBean; import io.swagger.annotations.Api; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile;@Api(tags = "測試") @RestController @RequestMapping("/test") public class TestController {@PostMapping("/email")@ResponseBodypublic RespBean show(@RequestParam("file")MultipartFile multipartFile){int num=new MailUtil().sendMail(multipartFile,"miluhang@126.com",null);if(num<=0){return RespBean.goBack(1,"發送失敗,請重試");}else{return RespBean.goBack(0,"已發送");}} }在此本人遇到535異常,此異常是因為讀取username默認讀取的本地名字,所以我在此加上email,535異常一般是因為賬號密碼寫錯
總結
以上是生活随笔為你收集整理的springBoot后台发送内容至邮箱的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腾讯公布第三季度财报 营收1546亿 净
- 下一篇: 微信即日起规范公众号群发诱骗点击类消息,