springboot发送邮件_Springboot(三十九)发送电子邮件

cc93171de19cf92a52fb882f9588c3cb.png

通过使用Spring Boot RESTful Web服务,可以发送包含Gmail传输层安全性的电子邮件。 在本章中,详细了解如何使用此功能。

首先,需要在构建配置文件中添加Spring Boot Starter Mail依赖项。

Maven用户可以将以下依赖项添加到pom.xml 文件中。

org.springframework.boot

spring-boot-starter-mail

Gradle用户可以在build.gradle 文件中添加以下依赖项。

compile('org.springframework.boot:spring-boot-starter-mail')

主 Spring Boot 应用程序类文件的代码如下 -

package com.felix.emailapp;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class EmailappApplication {

public static void main(String[] args) {

SpringApplication.run(EmailappApplication.class, args);

}

}

可以编写一个简单的Rest API,以便在Rest Controller类文件中发送到电子邮件,如图所示。

package com.felix.emailapp;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class EmailController {

@RequestMapping(value = "/sendemail")

public String sendEmail() {

return "Email sent successfully";

}

}

编写一个方法来发送带有附件的电子邮件。 定义mail.smtp属性并使用PasswordAuthentication。

private void sendmail() throws AddressException, MessagingException, IOException {

Properties props = new Properties();

props.put("mail.smtp.auth

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值