我一直试图通过spring boot发送电子邮件,我似乎无法成功。我一直在使用javamail api很长一段时间,但是想在春季邮件中使用springboot。
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 Tool Suite版本: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 --- [主要] com.example.demo.EmaildemoApplication:开始 带有PID 12576的Georges-MacBook-Pro.local上的EmaildemoApplication (由/ Users / georgetebo / STS Projects / Emaildemo中的georgetebo发起) 2017-09-14 05:25:57.814 INFO 12576 --- [主要] com.example.demo.EmaildemoApplication:没有活动的配置文件集, 回到默认配置文件:默认2017-09-14 05:25:57.845 INFO 12576 --- [主要] ationConfigEmbeddedWebApplicationContext:刷新 org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3d3fcdb0: 启动日期[Thu Sep 14 05:25:57 WAT 2017];上下文层次结构的根 2017-09-14 05:25:58.652 INFO 12576 --- [主要] s.b.c.e.t.TomcatEmbeddedServletContainer:初始化为Tomcat port(s):8080(http)2017-09-14 05:25:58.662 INFO 12576 --- [
main] o.apache.catalina.core.StandardService:启动服务 [雄猫] 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 INFO 12576 --- [ost-startStop-1] o.a.c.c.C. [Tomcat]。[localhost]。[/]: 初始化Spring嵌入式WebApplicationContext 2017-09-14 05:25:58.714 INFO 12576 --- [ost-startStop-1] o.s.web.context.ContextLoader:Root WebApplicationContext: 初始化完成时间为871 ms 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 INFO 12576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器: 'characterEncodingFilter'来:[/ ] 2017-09-14 05:25:58.824 INFO 12576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器:'hiddenHttpMethodFilter'到:[/ ] 2017-09-14 05:25:58.824 INFO 12576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器: 'httpPutFormContentFilter':[/ ] 2017-09-14 05:25:58.824 INFO 12576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean: 映射过滤器:'requestContextFilter'到:[/ ] 2017-09-14 05:25:58.848警告12576 --- [主要] ationConfigEmbeddedWebApplicationContext:遇到异常 在上下文初始化期间 - 取消刷新尝试: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为'examPle'的bean时出错:不满意的依赖项 通过字段'sendMail'表示;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 'com.example.demo.SendMail'类型的限定bean可用: 预计至少有1个豆有资格成为autowire候选人。 依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)} 2017-09-14 05:25:58.850 INFO 12576 --- [主要] o.apache.catalina.core.StandardService:停止服务[Tomcat] 2017-09-14 05:25:58.861 INFO 12576 --- [主要] utoConfigurationReportLoggingInitializer:启动ApplicationContext时出错。显示自动配置 报告在启用“debug”的情况下重新运行您的应用程序。 2017年9月14日 05: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
<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并安装了新的副本。现在我收到此错误
答案 0 :(得分:0)
您是否尝试过将javax.mail添加到您的pom? Spring电子邮件发件人对JavaMail具有依赖性。
答案 1 :(得分:0)
这很有效。
@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);
}}
答案 2 :(得分:0)
我认为您应该使用的密码是gmail的生成应用密码,它不应该只是我猜的任何密码。
答案 3 :(得分:-1)
似乎未正确获取您的属性,例如:
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);
从帖子Spring Boot Freemarker Email Template
中查看更多相关信息希望这有帮助!