gpt4 book ai didi

java - 通过创建 REST 端点使用 spring-boot 发送电子邮件

转载 作者:行者123 更新时间:2023-12-01 10:33:03 27 4
gpt4 key购买 nike

我正在尝试使用 Spring Boot 框架和 apache 服务器发送电子邮件我有以下 4 个文件。

application.properties

mail.host=smtp.gmail.com
mail.username=****
mail.password=****
mail.smtp.auth = true
mail.smtp.starttls.enable=true
mail.smtp.port = 25
#mail.smtp.ssl.enable=true

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Example</groupId>
<artifactId>SendingEmail</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
</parent>

<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>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.5</version>
</dependency>
</dependencies>

</project>

MailConfiguration.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

@Configuration
@PropertySource("classpath:application.properties")
public class MailConfiguration
{
@Value("${mail.host}")
private String host;
@Value("${mail.username}")
private String username;
@Value("${mail.password}")
private String password;
@Value("${mail.smtp.auth}")
private String auth;
@Value("${mail.smtp.port}")
private int port;
@Value("${mail.smtp.starttls.enable}")
private String fallback;
// @Value("${mail.smtp.ssl.enable}")
// private boolean ssl;

@Bean
public JavaMailSender javaMailSender()
{
JavaMailSenderImpl msender=new JavaMailSenderImpl();
Properties mailProperties=new Properties();
mailProperties.put("mail.smtp.auth",auth);
//mailProperties.put("mail.smtp.ssl.enable",ssl);
//mailProperties.put("spring.mail.properties.mail.smtp.socketFactory.class",socketclass);
mailProperties.put("mail.smtp.starttls.enable",fallback);
msender.setJavaMailProperties(mailProperties);
msender.setHost(host);
msender.setPort(port);
msender.setUsername(username);
msender.setPassword(password);
return msender;
}
}

MailSender.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MailSender
{
private final JavaMailSender javaMailSender;

@Autowired
MailSender(JavaMailSender javaMailSender)
{
this.javaMailSender = javaMailSender;
}

@RequestMapping("/send-mail")
SimpleMailMessage send()
{
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo("abc@gmail.com");
//mailMessage.setReplyTo("someone@localhost");
mailMessage.setFrom("xyz@gmail.com");
mailMessage.setSubject("Lorem ipsum");
mailMessage.setText("Lorem ipsum dolor sit amet [...]");
javaMailSender.send(mailMessage);
return mailMessage;
}
}

我已经创建了 REST 端点,并在访问 http://localhost:8080/send-mail 时运行 apache 服务器。 我收到 HTTP 状态 404 -/send-mail

最佳答案

您为 Gmail 使用了错误的 starttls 端口号。 587

关于java - 通过创建 REST 端点使用 spring-boot 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34980751/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com