SpringBoot实现简单发送邮件

了解邮件发送与接收过程

如下:

A—–1>—-2>—-3>—–B 包括三大步骤:
这里写图片描述

1) 计算机A通过SMTP协议把邮件发送到服务器S1。
2) 服务器S1再发送到服务器S2。
3) 计算机B通过POP3协议接收服务器S2上的邮件。

SMTP (Simple Mail Transfer Protocol): SMTP是电子邮件传输的互联网
标准,定义在RFC5321,默认使用端口 25
POP3 (Post Office Protocol - Version 3): POP3主要用于支持使用客户端远程管理在服务器上的电子邮件。定义在RFC 1939,为POP协议的第三版。
这两个协议均属于TCP/IP协议族的应用层协议,运行在TCP层之上。

SpringBoot发送邮件 application.properties 配置
本文使用126邮箱测试(需要在设置中去开启 smtp 服务器,设置授权码)

#字符集
spring.mail.default-encoding=UTF-8
#电子邮件地址
spring.mail.host=smtp.126.com
#Application
spring.application.name=SEND-MAIL
#授权密码
spring.mail.password=password
#邮箱服务器默认端口
spring.mail.port=25
#协议
spring.mail.protocol=smtp
#邮箱账号名
spring.mail.username=Email_Name
#SpringCloud 注册发现 配置
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#当前服务器端口
server.port=8081

pom.xml 配置

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
</parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- 表示为web工程 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- SpringBoot 发送邮件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <!-- SpringCloud 服务的注册与发现 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>

启动服务器 main

@SpringBootApplication
//用来被服务注册中心发现
@EnableEurekaClient
public class RunEmailService {
    public static void main(String[] args) {
        SpringApplication.run( RunEmailService.class, args);
    }
}

创建一个用于发送邮件及封装邮件信息的类

@RestController
public class SendEmail {
    @Autowired
    //用于发送邮件
    private JavaMailSender jms;
    @GetMapping("/sendEmail")
    public String sendEmail(String message){
            //用于封装邮件信息的实例
            SimpleMailMessage smm=new SimpleMailMessage();
            //由谁来发送邮件
            smm.setFrom("name1@126.com");
            //邮件主题
            smm.setSubject("Hello");
            //邮件内容
            smm.setText("Hello SpringBoot_Email");
            //接受邮件
            smm.setTo("name2o@gmail.com");
            try {
                jms.send(smm);
                return "发送成功";
            } catch (Exception e) {
                return "发送失败///"+e.getMessage();
        }
    }

启动服务器 main
运行 http://localhost:8081/sendEmail 发送邮件

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要在Spring Boot中实现发送邮件,您可以按照以下步骤进行操作: 1. 添加依赖:在您的项目的pom.xml文件中添加Spring Boot的邮件依赖,如下所示: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 配置邮件发送信息:在您的application.properties或application.yml文件中配置SMTP服务器和相应的认证信息,示例如下: ```properties # SMTP服务器地址 spring.mail.host=your-smtp-server # SMTP服务器端口 spring.mail.port=your-smtp-port # 邮件发送者用户名 spring.mail.username=your-username # 邮件发送者密码 spring.mail.password=your-password # 邮件发送者邮箱地址 spring.mail.from=your-email-address ``` 3. 创建邮件发送服务类:创建一个用于发送邮件的服务类,可以使用JavaMailSender类提供的方法来发送邮件。以下是一个简单的示例: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { @Autowired private JavaMailSender javaMailSender; public void sendEmail(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); javaMailSender.send(message); } } ``` 4. 在需要发送邮件的地方调用服务类:在您的代码中,通过@Autowired注解将邮件发送服务类注入到需要发送邮件的地方,并调用sendEmail方法发送邮件,示例如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @Autowired private EmailService emailService; @GetMapping("/sendEmail") public String sendEmail() { String to = "recipient@example.com"; String subject = "Test Email"; String text = "This is a test email."; emailService.sendEmail(to, subject, text); return "Email sent successfully."; } } ``` 这样,您就可以在Spring Boot应用程序中实现发送邮件功能了。请注意替换相应的SMTP服务器和认证信息,并根据您的需求进行修改和优化代码。希望对您有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值