springboot发送邮件

<!-- 邮件依赖 -->
     <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-mail</artifactId>
     </dependency>
     <!-- freemarker begin -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.27-incubating</version>
        </dependency>

全局配置文件:

# JavaMailSender 邮件发送的配置
spring.mail.host=smtp.qq.com
spring.mail.username=2694342466@qq.com
spring.mail.password=uuwzgchukjshddfe
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
package com.email.email;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class EmailConfig {
  @Value("${spring.mail.username}")
    private String emailForm;
    public String getEmailForm() {
        return emailForm;
    }

    public void setEmailForm(String emailForm) {
        this.emailForm = emailForm;
    }
}

package com.email.email;

import freemarker.template.TemplateException;

import javax.mail.MessagingException;
import java.io.File;
import java.io.IOException;

public interface EmailService {
    //发送简单邮件
    void sendSimpleMail(String sendTo,String title ,String cont);
    //发送带附件的邮件
    void sendAttachmentMail(String sendTo, String title , String cont, File file) throws MessagingException;
    public void sendTemplateMail(String sendTo, String title,String info) throws MessagingException, IOException, TemplateException;
}



service

package com.email.email;


import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;


import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class EmailServiceImpl  implements  EmailService{
    @Autowired
    private EmailConfig emailConfig;
   @Autowired
    private JavaMailSender mailSender;
@Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Override
    public void sendSimpleMail(String sendTo, String title,String cont) {
    //简单邮件的发送
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        mailMessage.setFrom(emailConfig.getEmailForm());
        mailMessage.setTo(sendTo);
        mailMessage.setSubject(title);
        mailMessage.setText(cont);
        mailSender.send(mailMessage);
    }
 //发送带附件的邮件
    @Override
    public void sendAttachmentMail(String sendTo, String title, String cont, File file) throws MessagingException {
        MimeMessage msg=mailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(msg,true);
        helper.setFrom(emailConfig.getEmailForm());
        helper.setTo(sendTo);
        helper.setSubject(title);
        helper.setText(cont);
        FileSystemResource r=new FileSystemResource(file);
        helper.addAttachment("附件",r);
        mailSender.send(msg);
    }
    @Override
    public void sendTemplateMail(String sendTo, String title,String info) throws MessagingException, IOException, TemplateException {
        MimeMessage msg = mailSender.createMimeMessage();
           MimeMessageHelper helper=new MimeMessageHelper(msg,true);
           helper.setFrom(emailConfig.getEmailForm());
           helper.setTo(sendTo);
           helper.setSubject(title);
           //封装模板数据
        Map<String,Object>model=new HashMap<>();
        //得到模板
       Template template=freeMarkerConfigurer.getConfiguration().getTemplate(info);
        String html= FreeMarkerTemplateUtils.processTemplateIntoString(template,model);
        helper.setText(html,true);
        mailSender.send(msg);
    }
}


controller

package com.email.email;

import freemarker.template.TemplateException;
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.ResponseBody;

import javax.mail.MessagingException;
import java.io.File;
import java.io.IOException;

@Controller
public class EmailControl {
    @Autowired
    private  EmailService emailService;
    @RequestMapping("/simple")
    @ResponseBody
    public String sendSimpleEmail(){
           emailService.sendSimpleMail("2694342466@qq.com","你好","明天我去你家玩");
           return "Success";
    }
    @RequestMapping("/attach")
    @ResponseBody
    public String sendAttachmentMail() throws MessagingException {
        File file=new File("src/main/resources/static/mysql (2).txt");
        emailService.sendAttachmentMail("2694342466@qq.com","你好","明天我去你家玩",file);
        return "Success";
    }
    @RequestMapping("/template")
    @ResponseBody
    public  String sendTemplateMail() throws MessagingException, IOException, TemplateException {
        emailService.sendTemplateMail("2694342466@qq.com","呵呵","info.html");
        return "Success";

    }
}




出现认证失败的解决方案:因为JDK1.8中jre\lib\security中两个 jar 包替换的缘故。将下载后的local_policy.jar和US_export_policy.jar替换到JDK1.8的jre\lib\security文件夹即可。
链接: https://pan.baidu.com/s/1hIdNn9FdsFT4i88OHmaeTA
提取码: m55q
整体结构
在这里插入图片描述

Spring Boot发送邮件通常通过JavaMail API实现,Spring Boot提供了一个方便的集成方式,无需额外配置SMTP服务器。以下是使用Spring Boot发送邮件的基本步骤: 1. 添加依赖:在`pom.xml`文件中添加Spring Boot Actuator和JavaMail的相关依赖,例如: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 配置邮箱服务:在application.properties或application.yml文件中设置SMTP服务器的信息,如主机名、端口、用户名、密码等: ```properties spring.mail.host=smtp.example.com spring.mail.port=587 spring.mail.username=your-email@example.com spring.mail.password=your-password spring.mail.protocol=smtp spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 3. 创建邮件消息:创建一个Java类,继承`AbstractMessageConverter`或使用`SimpleMailMessage`来构建邮件内容: ```java import org.springframework.mail.SimpleMailMessage; SimpleMailMessage message = new SimpleMailMessage(); message.setTo("recipient@example.com"); message.setFrom("sender@example.com"); message.setSubject("Hello from Spring Boot"); message.setText("This is a test email."); ``` 4. 使用Java配置或注解:在Spring Boot应用中,你可以使用Java配置类配置一个`JavaMailSender`实例,然后在需要的地方发送邮件: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.javamail.JavaMailSender; @Autowired private JavaMailSender javaMailSender; public void sendEmail(SimpleMailMessage message) { javaMailSender.send(message); } ``` 或者使用`@Autowired`自动注入并在方法上使用`@SendMail`注解: ```java @RestController public class EmailController { @Autowired private JavaMailSender javaMailSender; @PostMapping("/send-email") @SendMail public ResponseEntity<String> sendMessage(SimpleMailMessage message) { // ...处理并返回响应 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值