通过创建REST端点

时间:2016-01-24 19:59:36

标签: java spring maven spring-boot

我正在尝试使用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 - /发送邮件

1 个答案:

答案 0 :(得分:1)

你正在为gmail使用错误的starttls端口号。这是587