Springboot actual combat code using JavaMail to send mail package

The actual code of springboot [the package of sending mail using JavaMail] is ready to use.

1. Import dependencies

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

2. Join the configuration

spring:
  mail:
    host: xxx.com
    username: uuu
    password: pppp
    port: 25
    properties:
      mail:
        smtp:
		  auth: false
	# 下面这俩是我自己加的配置,springboot不存在
	# 发件人邮箱
    from: [email protected]
    nickname: JIMO

3. E-mail tools

import lombok.extern.slf4j.Slf4j;
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 javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * send mail
 *
 * @author jimo
 * @version 1.0.0
 */
@Slf4j
@Service
public class MailService {
    /**
     * mail sender
     */
    private final JavaMailSender mailSender;
    /**
     * from who
     */
    @Value("${spring.mail.from}")
    private String from;
    /**
     * from username
     */
    @Value("${spring.mail.nickname}")
    private String nickname;

    /**
     * 构造
     */
    public MailService(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    /**
     * send simple mail
     */
    public void sendSimpleMail(String subject, String body, String... to) {
        final SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom(from);
        msg.setSubject(subject);
        msg.setText(body);
        msg.setTo(to);
        mailSender.send(msg);
    }

    /**
     * send mime mail
     *
     * @param attachments 附件文件的绝对路径
     */
    public void sendMimeMail(String to, String subject, String body, List<String> attachments) {
        final MimeMessage msg = mailSender.createMimeMessage();
        try {
            final MimeMessageHelper helper = new MimeMessageHelper(msg, true);
            helper.setFrom(from, nickname);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(body, true);
            if (attachments != null && attachments.size() > 0) {
                for (String attachment : attachments) {
                    helper.addAttachment(attachment.substring(attachment.lastIndexOf(File.separator) + 1),
                            new FileSystemResource(attachment));
                }
            }
            mailSender.send(msg);
        } catch (Exception e) {
            log.error("mime邮件发送失败", e);
            throw new RuntimeException("发送邮件失败:" + e.getMessage());
        }
    }
}

If you want to send it to multiple people, use the refactoring function:

public void setTo(String[] to)

4. To be continued ~

Published 80 original articles · Like 319 · Visits 340,000+

Guess you like

Origin blog.csdn.net/jimo_lonely/article/details/105237984