eightechess
eightechess

Reputation: 56

Having troubles sending emails from spring boot application

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

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.

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

Upvotes: 0

Views: 12508

Answers (4)

Marc Colina
Marc Colina

Reputation: 1

I think the password you should use is the Generated app password for gmail, it shouldn't be just any password i guess.

Upvotes: 0

eightechess
eightechess

Reputation: 56

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

 }}

Upvotes: 0

David Pham
David Pham

Reputation: 1803

Seems your properties is not fetched properly, for example:

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

You should try to use other way like this:

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

See more this way from the post Spring Boot Freemarker Email Template

Hope this help!

Upvotes: -1

zlandorf
zlandorf

Reputation: 1130

Have you tried adding javax.mail to your pom ? Spring email sender has a dependency to JavaMail.

Upvotes: 0

Related Questions