java Spring Boot - 通过 Gmail 发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43998707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:52:19  来源:igfitidea点击:

Spring Boot - Sending Email via Gmail

javamavenemailspring-boot

提问by agata

I am trying to send an email using Spring boot, but I am keep getting the same error. I tried update maven, clean projects, I don't know what else can I do.

我正在尝试使用 Spring Boot 发送电子邮件,但我不断收到相同的错误。我试过更新 Maven,清理项目,我不知道我还能做什么。

Field serviceSendEmail in com.stopcozi.resource.AppointmentResource required 
a bean of type 'sendEmail.SendEmail' that could not be found.
Action:
Consider defining a bean of type 'sendEmail.SendEmail' in your configuration.

I've checked all the links and tried to configure a bean too, but I can't make it work. I've tried everything from here: Spring Boot 1.2.5.RELEASE - Sending E-mail via Gmail SMTP. I am using spring version: 1.4.5.RELEASE. Please take a look at my code, thanks a lot.

我检查了所有链接并尝试配置一个 bean,但我无法让它工作。我从这里尝试了一切:Spring Boot 1.2.5.RELEASE - Sending E-mail via Gmail SMTP。我使用的是 spring 版本:1.4.5.RELEASE。请看一下我的代码,非常感谢。

application.properties

应用程序属性

spring.mail.host: smtp.gmail.com
spring.mail.port: 587
spring.mail.username: [email protected]
spring.mail.password: xxx
spring.mail.properties.mail.smtp.auth: true
spring.mail.properties.mail.smtp.starttls.enable: true
spring.mail.properties.mail.smtp.starttls.required: true
spring.mail.properties.mail.smtp.ssl.enable = true
spring.mail.test-connection=true

pom.xml

pom.xml

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.5.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
 ....other dependencies

StopCoziApplication.java

停止CoziApplication.java

@SpringBootApplication
@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})  
public class StopCoziApplication {

public static void main(String[] args) {
    SpringApplication.run(StopCoziApplication.class, args);
}

@Bean
 public JavaMailSender javaMailService() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        javaMailSender.setHost("smtp.gmail.com");
        javaMailSender.setPort(587);

        javaMailSender.setJavaMailProperties(getMailProperties());
        javaMailSender.setUsername("[email protected]");
        javaMailSender.setPassword("xxx");

        return javaMailSender;
    }

    private Properties getMailProperties() {
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol", "smtp");
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.starttls.enable", "true");
        properties.setProperty("mail.debug", "true");
        properties.setProperty("mail.smtp.ssl.enable","true");
        properties.setProperty("mail.test-connection","true");
        return properties;
    }

SendEmail.java

发送电子邮件.java

@Service
public class SendEmail  {

@Autowired
private JavaMailSender javaMailSender;

@PostConstruct
public void sendMail(String to,String body) {

    System.out.println("Sending email...");

    SimpleMailMessage message = new SimpleMailMessage();
    message.setTo(to);
    message.setFrom("[email protected]");
    message.setSubject("Confirm appointment");
    message.setText(body);
    javaMailSender.send(message);

    System.out.println("Email Sent!");
    }

 }

add this is the class where I make the call AppointmentResource.java

添加这是我调用 AppointmentResource.java 的类

public class AppointmentResource {

    @Autowired
    private AppointmentService appointmentService;

    @Autowired
    SendEmail serviceSendEmail;

    @RequestMapping("/{id}/confirm")
    public void confirmAppointment(@PathVariable("id") Long id) {
        appointmentService.confirmAppointment(id);
        Appointment appointment = appointmentService.findAppointment(id);
        String email = appointment.getUser().getEmail();
        serviceSendEmail.sendMail(email, "Your appointment was confirmed"
        +appointment.getAgency()+" service "+appointment.getService()+" "
                + "date "+appointment.getDate()+ " Have a nice day!");
    }
}

enter image description here

在此处输入图片说明

回答by agata

The answer:

答案:

As @Jesper suggested, the problem was I didn't put the SendEmail class in in the same package or a subpackage of my application class. What I did was put the SendEmail.java in a subpackage (com.stopcozi.sendEmail).

正如@Jesper 所建议的,问题是我没有将 SendEmail 类放在我的应用程序类的同一个包或子包中。我所做的是将 SendEmail.java 放在一个子包 (com.stopcozi.sendEmail) 中。

I didn't create and configure @Bean manually, I just let spring boot do the job. Need only SendEmail.java in the right package and the application.properties. The rest (AppointmentResource.java and pom.xml) were ok. In app.properties I used a different port.
application.properties

我没有手动创建和配置@Bean,我只是让 spring boot 来完成这项工作。只需要正确包中的 SendEmail.java 和 application.properties。其余的(AppointmentResource.java 和 pom.xml)没问题。在 app.properties 中,我使用了不同的端口。
应用程序属性

#spring-boot-starter-mail properties
spring.mail.host: smtp.gmail.com
spring.mail.port: 465
spring.mail.username: [email protected]
spring.mail.password: xxx
spring.mail.properties.mail.smtp.auth: true
spring.mail.properties.mail.smtp.starttls.enable: true
spring.mail.properties.mail.smtp.starttls.required: true
spring.mail.properties.mail.smtp.ssl.enable: true
spring.mail.test-connection: true