spring cloud注册中心实现发送邮件

1.服务端配置

引入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>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
</dependencies>

创建一个application.yml

server: 
  port: 8761
eureka:
  instance:
    statusPageUrlPath: ${management.context-path}/info
    healthCheckUrlPath: ${management.context-path}/health

启动项目

@SpringBootApplication
@EnableEurekaServer
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

2. 客户端的配置

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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
</dependencies>

创建一个application.properties

spring.mail.default-encoding=UTF-8
spring.mail.host= smtp.126.com
spring.mail.password= lgw1993
spring.mail.port= 25
spring.mail.protocol=smtp
spring.mail.username= lgw_999
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
spring.application.name=EMAIL_02
server.port=8082

发送邮件

@RestController
public class MailController {
    @Autowired
    private JavaMailSender sender;
    @GetMapping("/send")
    public String send(){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("lgw_999@126.com");
        message.setTo("1091835363@qq.com");
        message.setSubject("这是一份测试邮件");
        message.setText("请点击注册");
        sender.send(message);
        return "1";
    }
}

启动项目

@SpringBootApplication
@EnableEurekaClient
public class Main { 
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值