New file |
| | |
| | | package com.vincent.rsf.server.common.service; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.mail.MailException; |
| | | import org.springframework.mail.javamail.JavaMailSender; |
| | | import org.springframework.mail.javamail.MimeMessageHelper; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.mail.MessagingException; |
| | | import javax.mail.internet.MimeMessage; |
| | | |
| | | @Slf4j |
| | | @Service |
| | | public class EmailService { |
| | | |
| | | @Value("${spring.mail.username}") |
| | | private String from; |
| | | |
| | | @Autowired |
| | | private JavaMailSender mailSender; |
| | | |
| | | public boolean sendVerificationEmail(String to, String verificationCode) { |
| | | try { |
| | | MimeMessage message = mailSender.createMimeMessage(); |
| | | message.setFrom(from); |
| | | MimeMessageHelper helper = new MimeMessageHelper(message, true); |
| | | |
| | | // 邮件主题和内容 |
| | | helper.setTo(to); |
| | | helper.setSubject("Email Verification Code"); |
| | | helper.setText("Your verification code is: " + verificationCode); |
| | | |
| | | mailSender.send(message); |
| | | return true; |
| | | } catch (MailException | MessagingException e) { |
| | | log.error(this.getClass().getSimpleName(), e); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | } |