使用SpringBoot实现电子邮件发送

本文详细介绍了如何在SpringBoot项目中实现电子邮件发送功能,包括添加依赖、配置邮件发送器、编写邮件发送逻辑及控制器,并提供了测试方法。

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

使用SpringBoot实现电子邮件发送

1、在pox.xml中加入相关包

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

如果使用的是spring项目,则换成springFramework对应的相关jar包即可


2、编写邮件发送逻辑

package com.example.service;

import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

/**
* 邮件发送业务逻辑
* @author yfly
* @date 2018年5月23日 上午11:28:48
*/
@Service
public class MailSenderService implements InitializingBean {
   
   //加入日志  
   private static final Logger logger = LoggerFactory.getLogger(MailSenderService.class);
   //使用该接口发送邮件
   private JavaMailSenderImpl mailSender;
   /**
    * 发送邮件
    * @param to
    */
   public void sendMail(String to){
   	SimpleMailMessage mailMessage = new SimpleMailMessage();
   	//发送方邮箱
   	mailMessage.setFrom("yflyfox@163.com");
   	//接收方邮箱
   	mailMessage.setTo(to);
   	//发送的邮件主题
   	mailMessage.setSubject("spring 测试邮件发送");
   	//发送的邮件内容
   	mailMessage.setText("这是用Spring框架发送的邮件!,仅做测试使用");//后期通过值传递,注入
   	mailSender.send(mailMessage);
   }
   /**
    * 配置邮件发送器
    * @throws Exception
    */
   @Override
   public void afterPropertiesSet() throws Exception {
   	mailSender = new JavaMailSenderImpl();
   	//用户名
   	mailSender.setUsername("yflyfox@163.com");
   	//SMTP客户端的授权码(每个人这个密码不一样,密码与上面的username对应密码相同)
   	mailSender.setPassword("*******");
   	// 发件人邮箱的 SMTP 服务器地址
   	mailSender.setHost("smtp.163.com");
   	//邮件服务器监听的端口
   	mailSender.setPort(465);
   	//协议SMTP+SSL
   	mailSender.setProtocol("smtps");
   	mailSender.setDefaultEncoding("utf8");
   	Properties javaMailProperties = new Properties();
   	javaMailProperties.put("mail.smtp.ssl.enable", true);
   	mailSender.setJavaMailProperties(javaMailProperties);
   }
}

3、编写电子邮件发送控制器

package com.example.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.service.MailSenderServic;

@Controller
public class MailSenderController {
   private static final Logger logger = LoggerFactory.getLogger(MailSenderController.class);
   @Autowired
   MailSenderServic mailSender;
   @RequestMapping(path = {"/mail"}, method = {RequestMethod.POST})
   @ResponseBody
   public String emailSend(){
   	try {
   		mailSender.sendMail("1456939892@qq.com");
   		logger.info("——————————>邮件发送成功");
   		return "邮件发送成功";
   	}catch (Exception e){
   		logger.error("邮件发送失败: " + e.getMessage());
   		return "邮件发送失败";
   	}
   }
}

4、使用浏览器测试

localhost:8080/mail

推荐使用Postman工具测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值