SpringBoot实现发送邮件

本文详细介绍了如何在SpringBoot项目中整合邮件发送功能,包括配置SMTP服务器、编写启动类、实现简单邮件、HTML邮件及带附件邮件的发送,并提供了测试类的编写方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.编写启动类

@SpringBootApplication
public class MailApplication {

    public static void main(String[] args) {
        SpringApplication.run(MailApplication.class, args);
    }
}

2.配置properties

spring.mail.host=smtp.126.com
### from email
spring.mail.username=xxxx@126.com
### from email 代理密码   
spring.mail.password=xxxxx
spring.mail.default-encoding=utf-8

#### to email
to_email=xxxxxxx@126.com

3.实现发送邮件

@Service
public class MailService {

    @Value("${spring.mail.username}")
    private String from;
    @Resource
    private JavaMailSender javaMailSender;

    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        message.setFrom(from);

        javaMailSender.send(message);
    }

    /**
     * 发送html格式的邮件
     * @param to 接受者
     * @param subject 主题
     * @param content 内容
     */
    public void sendHtmlMail(String to, String subject, String content) {
        MimeMessage message=javaMailSender.createMimeMessage();
        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper=new MimeMessageHelper(message,true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content,true);
            javaMailSender.send(message);
            System.out.println("html格式邮件发送成功");
        }catch (Exception e){
            System.out.println("html格式邮件发送失败");
        }
    }

    /**
     * 发送带附件的邮件
     * @param to 接受者
     * @param subject 主题
     * @param content 内容
     * @param filePath 文件路径
     */
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
        MimeMessage message=javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper=new MimeMessageHelper(message,true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content);
            FileSystemResource file=new FileSystemResource(new File(filePath));
            String fileName=filePath.substring(filePath.lastIndexOf(File.separator));
            //添加多个附件可以使用多条
            //helper.addAttachment(fileName,file);
            helper.addAttachment(fileName,file);
            javaMailSender.send(message);
            System.out.println("带附件的邮件发送成功");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("发送带附件的邮件失败");
        }
    }

4.编写测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailServiceTest {

    @Autowired
    private MailService mailService;

    @Test
    public void sendSimpleMail() {
       mailService.sendSimpleMail("xxxx@qq.com","吃饭","今天你吃饭了吗");
    }

    @Test
    public void sendHtmlMail() {
        String to = "xxxxx@qq.com";
        String subject = "大明湖";
        String content="<html>\n"+"<body>\n"
                + "<h3>hello world!测试发送html格式邮件</h3>\n"
                +"</body>\n"+"</html>";
        mailService.sendHtmlMail(to,subject,content);
    }

    @Test
    public  void sendAttachmentsMail(){
        String to = "xxxxx@qq.com";
        String filePath="C:\\Users\\ASUS\\Pictures\\SharedImageServer\\contentpic\\2.jpg";
        mailService.sendAttachmentsMail(to,"带附件的邮件","有附件,请查收",filePath);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值