springboot 使用邮件服务发送验证码 以及在阿里云服务器的配置

pom需要引入spring-boot-starter-mail

如下:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

我这里用的是126邮箱 163应该是同理的

配置文件 application.properties

端口设置为465、开启ssl协议类型为smtp (在阿里云部署需要配置出入规则的465端口。我是把出和入的端口都打开了)按照这个配置是能在阿里云调用通的

我刚开始没有配置 端口设置为465、开启ssl、协议类型为smtp 在本地可以调通能发邮件,到阿里云服务器不行

#我用的是126邮箱
spring.mail.host=smtp.126.com
#这里配置你的邮箱 spring.mail.username
=nideyouxiang@126.com spring.mail.password=开启smtp时候的授权码 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.starttls.enable=true spring.mail.properties.mail.starttls.required=true spring.mail.properties.mail.smtp.port=465 spring.mail.protocol=smtp spring.mail.properties.mail.smtp.ssl.enable=true

stmp授权码图(邮箱设置里边开启的):

 

 

 

 

代码:

上边pom的spring-boot-starter-mail引入后你能使用

JavaMailSender 是mail包提供的
import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import sinosoft.core.exception.ServiceException;
import sinosoft.sys.modular.user.enums.SysUserExceptionEnum;

@Service
public class MailService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private JavaMailSender sender;
    @Value("${spring.mail.username}")
    private String formMail;


    /**
     * 发送简单的邮件信息
     * @param toMail
     * @param subject
     * @param content
     */
    public void sendSimpleMail(String toMail,String subject,String content) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(formMail);
        simpleMailMessage.setTo(toMail);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        try {
            sender.send(simpleMailMessage);
            logger.info("发送给"+toMail+"邮件已经发送。 subject:"+subject);
        } catch (Exception e) {
            logger.info("发送给"+toMail+"send mail error subject:"+subject);
            e.printStackTrace();
        }
    }
    /**
     * 发送html格式的邮件 
     * @param toMail
     * @param subject
     * @param content
     */
    public void sendHtmlMail(String toMail,String subject,String content) {
        MimeMessage createMimeMessage = sender.createMimeMessage();
        
        try {
            //true表示需要创建一个multipart message 如何不设置就不会解析html
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(createMimeMessage,true);
            mimeMessageHelper.setFrom(formMail);
            mimeMessageHelper.setTo(toMail);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(content, true);
            sender.send(createMimeMessage);
            logger.info("发送给"+toMail+"html邮件已经发送。 subject:"+subject);
        } catch (MessagingException e) {
            logger.info("发送给"+toMail+"html send mail error subject:"+subject);
            e.printStackTrace();
            
        }
    }
     
    
    
    public void sendAttachmentsMail(String toMail,String subject,String content,String filePath) {
            MimeMessage message = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(formMail);
            helper.setTo(toMail);
            helper.setSubject(subject);
            helper.setText(content, true);
            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf("/"));
            helper.addAttachment(fileName, file);
            sender.send(message);
            logger.info("发送给"+toMail+"带附件的邮件已经发送。");
        } catch (MessagingException e) {
            e.printStackTrace();
            logger.error("发送给"+toMail+"带附件的邮件时发生异常!", e);
        }
    }

}

 

然后你就可以在你的业务代码中调上边封装的MailService了

比如发送简单的邮件消息(我的业务代码):

/**
     * 邮件服务
     */
    @Resource
    private MailService mailService;

//toEmail-要发送的邮箱、systemName-邮件的标题、content-邮件的内容
mailService.sendSimpleMail(toEmail, systemName, content);

 

posted @ 2021-12-31 14:47  liglacier  阅读(459)  评论(0编辑  收藏  举报