赞
踩
在yml文件中进行配置
mail: # host不配置会注入失败 debug: true host: xxxxx port: 25 username: xxxx password: xxxx #default-encoding: UTF-8 #protocol: smtp properties: mail: smtp: auth: true ssl: enable: true # required: true
注意mail一定要配置在
spring:下面,否则装配的时候会报找不到这个bean
@Service public class MailServiceImpl implements MailService { @Autowired JavaMailSender javaMailSender; @Autowired AppNoticeDetailRepository appNoticeDetailRepository; //简单邮件发送 @Override public void sendSimpleSender(String from,String[] to,String cc,String subject,String content){ SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setFrom(from); simpleMailMessage.setTo(to); simpleMailMessage.setCc(cc); simpleMailMessage.setSubject(subject); simpleMailMessage.setText(content); javaMailSender.send(simpleMailMessage); } @Override public void sendMail(MailVo mailVo){ SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setFrom(mailVo.getFromMail()); simpleMailMessage.setTo(mailVo.getToMail()); simpleMailMessage.setSubject(mailVo.getTitle()); simpleMailMessage.setText(mailVo.getContent()); javaMailSender.send(simpleMailMessage); } @Override public void sendMailByNoticeDetailId(Long noticeDetailId){ AppNoticeDetail appNoticeDetail=appNoticeDetailRepository.searchById(noticeDetailId); SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); String personId=appNoticeDetail.getPersonId(); simpleMailMessage.setFrom(TaskStatus.NOTICE_NOTREAD); simpleMailMessage.setTo(personId.split(",")); simpleMailMessage.setSubject("通知"); simpleMailMessage.setText(appNoticeDetail.getContent()); javaMailSender.send(simpleMailMessage); } }
当发送给多个人邮件的时候,默认为密送模式,可以采用数组的方式进行邮件的发送
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。