实现SpringBoot发送mail邮件

前言

邮件服务在注册绑定账号或者信息通知的时候,是非常有必要的.本文介绍springboot发送邮件服务.让各位看官能亲自体验,也能很方便的接入到项目中.

准备工作

1:开启163邮箱POP3/SMTP/IMAP服务:

首先登陆自己的163账号.找到设置
在这里插入图片描述
通过指示发送短信开启服务,会弹出授权码.(只会展示一次请保管好).

2:开启QQ邮箱POP3/SMTP/IMAP服务:

设置
通过指示发送短信开启服务,会弹出授权码.(只会展示一次请保管好).

类型服务器名称服务器地址SSL协议端口号非SSL协议端口号
收件服务器POPpop.163.com995110
收件服务器IMAPimap.163.com993143
收件服务器SMTPsmtp.163.com465/99425

SpringBoot 配置准备:

springboot 自带邮件发送服务,引入配置pom:

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

yml配置:

  mail:
      # qq
      # host: smtp.qq.com
	  # 163
      host: smtp.163.com
      port: 587
      username: XXXXXXX@163.com
#      password: 此密码为 上面服务启用的授权码
      password: XXXXXXX
      protocol: smtp
      default-encoding: UTF-8
      properties:
          mail.smtp.auth: true
          mail.smtp.starttls.enable: true
          mail.smtp.starttls.required: true
          mail.smtp.socketFactory.port: 465
          mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory
          mail.smtp.socketFactory.fallback: false

编码实现

1: 邮件发送代码实现:

/**
     *  发送邮件
     * @param to 发送到
     * @param subject 主题
     * @param text 内容
     */
    @Async
    @Override
    public void sendMail(String to, String subject, String text) {

        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper =  new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true);
            mailSender.send(message);
            log.info("发送邮件成功");
        } catch (MessagingException e) {
            log.error("发送邮件失败",e.toString());
            throw new BusinessException("发送邮件失败");
        }
    }

2: 带附件的邮件发送:

  /**
     * 发送邮件
     *
     * @param to          发送到
     * @param subject     主题
     * @param text        内容
     * @param attachFiles 附件
     */
    @Async
    @Override
    public void sendMailAttachments(String to, String subject, String text, List<String> attachFiles) {

        MimeMessage message=mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper=new MimeMessageHelper(message,true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true);
            //验证附件数据是否为空
            if(null != attachFiles){
                FileSystemResource file=null;
                for (String attachFile : attachFiles) {
                    //添加附件
                    file = new FileSystemResource(attachFile);
                    helper.addAttachment(attachFile.substring(attachFile.lastIndexOf("/")+1), file);
                }
            }
            mailSender.send(message);
            log.info("带附件的邮件发送成功");
        }catch (Exception e){
            log.error("发送带附件的邮件失败",e.toString());
            throw new BusinessException("发送邮件失败");
        }
    }

前段案列

基于邮件发送实现案列demo,结合前段让看官可以亲自体验测试:
案列
在这里插入图片描述
发送后收到邮件如下:
在这里插入图片描述
体验地址: http://132.232.43.102
账号:guest
密码:guest
常见案列 -> 发送邮件

后续介绍 freemarker模板发送邮件服务

  • 13
    点赞
  • 112
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在Spring Boot中使用邮件发送Excel附件,你可以按照以下步骤进行操作: 1. 首先,你需要在Spring Boot项目的依赖中添加邮件发送的相关库。你可以在pom.xml文件中添加如下代码: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 接下来,在你的配置文件(application.properties 或 application.yml)中配置邮件发送的相关信息,例如: ```yaml spring.mail.host=smtp.163.com spring.mail.port=25 spring.mail.username=your-email@example.com spring.mail.password=your-email-password ``` 3. 在你的代码中,你可以使用JavaMailSender来发送邮件。你可以创建一个邮件附件,并将其添加到邮件中。下面是一个示例方法: ```java @Autowired private JavaMailSender mailSender; public void sendEmailWithAttachment() throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo("recipient@example.com"); helper.setSubject("Spring Boot Mail with Excel Attachment"); helper.setText("Please find the attached Excel file."); // 创建邮件附件 FileSystemResource file = new FileSystemResource(new File("path/to/excel/file.xlsx")); helper.addAttachment("excel-file.xlsx", file); mailSender.send(message); } ``` 在上面的示例中,你需要将"recipient@example.com"替换为收件人的邮箱地址,并将"path/to/excel/file.xlsx"替换为你要发送的Excel文件的路径。 这样,在调用sendEmailWithAttachment()方法时,就会发送包含Excel附件的邮件了。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [springboot 发送带excel附件的邮件](https://blog.csdn.net/weixin_41722928/article/details/105513055)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [技术栈:SpringBoot+Mybatis-plus+Mybatis+thymeleaf+MySQL](https://download.csdn.net/download/Abelon/88245984)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值