本文介绍了从 Spring Boot 应用程序发送电子邮件时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

公共类 SendMail {@自动连线私人 JavaMailSender javaMailSender;公共无效发送邮件(字符串到,字符串主题,字符串正文){SimpleMailMessage message = new SimpleMailMessage();message.setTo(to);message.setSubject(主题);message.setText(body);javaMailSender.send(message);}}

这是我的邮件

@Controller公共类示例{@自动连线私人 SendMail sendMail;@RequestMapping("/他")公共字符串主页(){sendMail.sendingMail("[email protected]", "欢迎乔治", "示例消息");返回已发送";}}

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

属性文件

spring.mail.host=smtp.gmail.comspring.mail.username=我的电子邮件地址spring.mail.password=我的密码spring.mail.port=587spring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.starttls.enable=truespring.mail.defaultEncoding=UTF-8

2017-09-14 05:25:57.811 INFO 12576 --- [主要]com.example.demo.EmaildemoApplication :开始Georges-MacBook-Pro.local 上的电子邮件演示应用程序,PID 为 12576(由 georgetebo 在/Users/georgetebo/STS Projects/Emaildemo 中启动)2017-09-14 05:25:57.814 信息 12576 --- [主要]com.example.demo.EmaildemoApplication :未设置活动配置文件,回退到默认配置文件:默认 2017-09-14 05:25:57.845信息 12576 --- [主要]ationConfigEmbeddedWebApplicationContext : 刷新org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3d3fcdb0:启动日期 [2017 年 9 月 14 日星期四 05:25:57 WAT];上下文层次结构的根2017-09-14 05:25:58.652 信息 12576 --- [主要]s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat 初始化为端口:8080 (http) 2017-09-14 05:25:58.662 INFO 12576 --- [
main] o.apache.catalina.core.StandardService : 启动服务[Tomcat] 2017-09-14 05:25:58.662 INFO 12576 --- [主要]org.apache.catalina.core.StandardEngine :启动 Servlet 引擎:Apache Tomcat/8.5.20 2017-09-14 05:25:58.714 信息 12576 ---[ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] :初始化 Spring 嵌入式 WebApplicationContext 2017-09-1405:25:58.714 信息 12576 --- [ost-startStop-1]o.s.web.context.ContextLoader : 根 WebApplicationContext:初始化在 871 毫秒内完成 2017-09-14 05:25:58.821 INFO 12576--- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : 将 servlet: 'dispatcherServlet' 映射到 [/] 2017-09-14 05:25:58.823信息 12576 --- [ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean : 映射过滤器:'characterEncodingFilter' to: [/] 2017-09-14 05:25:58.824 INFO 12576--- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean :映射过滤器:'hiddenHttpMethodFilter'到:[/] 2017-09-1405:25:58.824 信息 12576 --- [ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean : 映射过滤器:'httpPutFormContentFilter' 到:[/] 2017-09-14 05:25:58.824 INFO12576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean :映射过滤器:'requestContextFilter' 到:[/] 2017-09-1405:25:58.848 警告 12576 --- [主要]ationConfigEmbeddedWebApplicationContext : 遇到异常在上下文初始化期间 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为example"的 bean 时出错:依赖项不满足通过字段sendMail"表示;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:否'com.example.demo.SendMail' 类型的合格 bean 可用:预计至少有 1 个 bean 有资格作为自动装配候选者.依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}2017-09-14 05:25:58.850 信息 12576 --- [主要]o.apache.catalina.core.StandardService : 停止服务 [Tomcat]2017-09-14 05:25:58.861 信息 12576 --- [主要]utoConfigurationReportLoggingInitializer :

启动 ApplicationContext 时出错.显示自动配置报告在启用调试"的情况下重新运行您的应用程序.2017-09-1405:25:58.927 错误 12576 --- [主要]o.s.b.d.LoggingFailureAnalysisReporter:

*************************** 应用程序无法启动

说明:

com.example.demo.Example 中的字段 sendMail 需要一个 bean 类型找不到com.example.demo.SendMail".

操作:

考虑定义一个类型为 'com.example.demo.SendMail' 的 bean配置.

这是我的 Pom 文件

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

<artifactId>MailSender</artifactId><version>0.0.1-SNAPSHOT</version><包装>罐</包装><name>MailSender</name><description>ZemoPoint for Spring Boot</description><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.6.RELEASE</version><relativePath/><!-- 从存储库中查找父级 --></父母><属性><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></属性><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><范围>测试</范围></依赖></依赖项><构建><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></插件></plugins></build>

我已经卸载了 jdk 和 STS 并安装了新副本.现在出现此错误

解决方案

这有效.

@Service公共类 SendMail {@自动连线私人 JavaMailSender javaMailSender;公共无效发送邮件(字符串到,字符串主题,字符串正文){MimeMessage 消息=javaMailSender.createMimeMessage();MimeMessageHelper 助手;helper=new MimeMessageHelper(message,true);helper.setTo(to);helper.setSubject(主题);helper.setText(body);javaMailSender.send(message);}}

I have been trying to send email via spring boot and i cant seem to succeed. I am been using javamail api for a long time now but want to use springboot with spring mail.

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);
    }

}

and here is my mailer

@Controller
public class ExamPle {

    @Autowired
    private SendMail  sendMail;

    @RequestMapping("/he")
    public String homePage() {
        sendMail.sendingMail("[email protected]", "Welcome George", "Sample Message here");
        return "Sent";
        }
}

I already have sprint-boot-starter-mail in my pom file but i keep getting this error message. I have even watched youtube videos of tutorials on how to do this and yet it doesn't work. I am using Spring Tool Suite Version: 3.9.0.RELEASE.

Properties file

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

This is my Pom file

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>

I have uninstalled my jdk as well as STS and installed new copies.Now i get this error

解决方案

This works.

@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);

 }}

这篇关于从 Spring Boot 应用程序发送电子邮件时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 01:02