从 spring 引导应用程序发送电子邮件时遇到问题

Having troubles sending emails from spring boot application

我一直在尝试通过 spring 引导发送电子邮件,但我似乎无法成功。我已经使用 javamail api 很长时间了,但想使用 springboot 和 spring 邮件。

public class SendMail {

    @Autowired
    private JavaMailSender javaMailSender;

    public void sendingMail(String to, String subject, String body) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(body);
        javaMailSender.send(message);
    }

}

这是我的邮件

@Controller
public class ExamPle {

    @Autowired
    private SendMail  sendMail;

    @RequestMapping("/he")
    public String homePage() {
        sendMail.sendingMail("bobobush007@gmail.com", "Welcome George", "Sample Message here");
        return "Sent";
        }
}

我的 pom 文件中已经有 sprint-boot-starter-mail,但我一直收到此错误消息。我什至看过有关如何执行此操作的教程的 youtube 视频,但它不起作用。我正在使用 Spring 工具套件版本:3.9.0.RELEASE.

属性文件

spring.mail.host=smtp.gmail.com
spring.mail.username=my-email-address
spring.mail.password=my-password
spring.mail.port=587
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.defaultEncoding=UTF-8

2017-09-14 05:25:57.811 INFO 12576 --- [ main] com.example.demo.EmaildemoApplication : Starting EmaildemoApplication on Georges-MacBook-Pro.local with PID 12576 (started by georgetebo in /Users/georgetebo/STS Projects/Emaildemo) 2017-09-14 05:25:57.814 INFO 12576 --- [ main] com.example.demo.EmaildemoApplication : No active profile set, falling back to default profiles: default 2017-09-14 05:25:57.845 INFO 12576 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3d3fcdb0: startup date [Thu Sep 14 05:25:57 WAT 2017]; root of context hierarchy 2017-09-14 05:25:58.652 INFO 12576 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2017-09-14 05:25:58.662 INFO 12576 --- [
main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2017-09-14 05:25:58.662 INFO 12576 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.20 2017-09-14 05:25:58.714 INFO 12576 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2017-09-14 05:25:58.714 INFO 12576 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 871 ms 2017-09-14 05:25:58.821 INFO 12576 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2017-09-14 05:25:58.823 INFO 12576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/] 2017-09-14 05:25:58.824 INFO 12576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/] 2017-09-14 05:25:58.824 INFO 12576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/] 2017-09-14 05:25:58.824 INFO 12576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/] 2017-09-14 05:25:58.848 WARN 12576 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'examPle': Unsatisfied dependency expressed through field 'sendMail'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.SendMail' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2017-09-14 05:25:58.850 INFO 12576 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2017-09-14 05:25:58.861 INFO 12576 --- [ main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2017-09-14 05:25:58.927 ERROR 12576 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :

*************************** APPLICATION FAILED TO START


Description:

Field sendMail in com.example.demo.ExamPle required a bean of type 'com.example.demo.SendMail' that could not be found.

Action:

Consider defining a bean of type 'com.example.demo.SendMail' in your configuration.

这是我的 Pom 文件

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.mailsender</groupId>
<artifactId>MailSender</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>MailSender</name>
<description>ZemoPoint for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<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.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

我已经卸载了我的 jdk 和 STS 并安装了新的 copies.Now 我收到这个错误

您是否尝试过将 javax.mail 添加到您的 pom 中? Spring 电子邮件发件人依赖于 JavaMail。

似乎您的属性未正确获取,例如:

spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

您应该尝试使用其他方式:

JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

mailSender.setHost(emailProperties.getHost());
mailSender.setPort(Integer.parseInt(emailProperties.getPort()));
mailSender.setUsername(emailProperties.getUsername());
mailSender.setPassword(emailProperties.getPassword());

Properties javaMailProperties = new Properties();
javaMailProperties.put("mail.smtp.starttls.enable", "true");
javaMailProperties.put("mail.smtp.auth", "true");
javaMailProperties.put("mail.transport.protocol", "smtp");
javaMailProperties.put("mail.debug", "true");

mailSender.setJavaMailProperties(javaMailProperties);

从 post Spring Boot Freemarker Email Template

中以这种方式查看更多内容

希望对您有所帮助!

这有效。

@Service
public class SendMail {

@Autowired
private JavaMailSender javaMailSender;

  public void sendingMail(String to, String subject, String body) {
    MimeMessage message=javaMailSender.createMimeMessage();
    MimeMessageHelper helper;
    helper=new MimeMessageHelper(message,true);
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText(body);
    javaMailSender.send(message);

 }}

我认为您应该使用的密码是为 gmail 生成的应用程序密码,它不应该只是我猜的任何密码。