1、首先创建maven项目,配置pom文件
2、配置springboot,我这里使用的是properties方式
#配置Mybatis别名和扫描包mybatis.type-aliases-package=com.demo.beanmybatis.mapper-locations=classpath:mapper/*.xml #数据库相关spring.datasource.url=jdbc:mysql://localhost:3306/ssm?useSSL=falsespring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver #配置日志logging.level.root=infologging.level.com.demo.mapper=debug #配置视图前缀和后缀spring.mvc.view.prefix=/spring.mvc.view.suffix=.html #邮件发送配置spring.mail.default-encoding=UTF-8spring.mail.host=smtp.qq.comspring.mail.username=你的邮箱spring.mail.password=邮箱授权码spring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.smtp.starttls.required=true #thymeleaf配置spring.thymeleaf.mode=HTML5spring.thymeleaf.encoding=UTF-8spring.thymeleaf.servlet.content-type=text/htmlspring.thymeleaf.cache=false
邮箱授权码可以按以下方法获取打开QQ邮箱网页→设置→账户→POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务→开启POP3/SMTP服务,然后就能看到授权码了
3、编写mailService
${spring.mail.username}是在properties中配置的属性,这里有一个方法,第一个是发送普通邮件,第二个是发送带有附件的邮件
@Service("mailService")public class MailService { @Value("${spring.mail.username}") private String from; @Autowired private JavaMailSender mailSender; Logger logger = LoggerFactory.getLogger(this.getClass()); public void sendSimpleMail(String to,String title,String content){ SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(title); message.setText(content); mailSender.send(message); logger.info("邮件发送成功"); } public void sendAttachmentsMail(String to, String title, String cotent, List
4、编写controller
@Controllerpublic class MailController { @Autowired private MailService mailService; @RequestMapping("getCheckCode") @ResponseBody public String getCheckCode(String email){ String checkCode = String.valueOf(new Random().nextInt(899999) + 100000); String message = "您的注册验证码为:"+checkCode; try { mailService.sendSimpleMail(email, "注册验证码", message); }catch (Exception e){ return ""; } return checkCode; }}
5、编写页面
上一篇:C/C++中怎么实现树操作
下一篇:java怎样实现俄罗斯方块游戏