java - Sending email using spring-boot by creating REST endpoint -


i trying send email using spring boot framework , using apache server have following 4 file.

application.properties

mail.host=smtp.gmail.com mail.username=**** mail.password=**** mail.smtp.auth = true mail.smtp.starttls.enable=true mail.smtp.port = 25 #mail.smtp.ssl.enable=true 

pom.xml

<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0"          xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"          xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelversion>4.0.0</modelversion>      <groupid>example</groupid>     <artifactid>sendingemail</artifactid>     <version>1.0-snapshot</version>      <parent>     <groupid>org.springframework.boot</groupid>     <artifactid>spring-boot-starter-parent</artifactid>     <version>1.3.2.release</version>     </parent>      <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>javax.mail</groupid>             <artifactid>mail</artifactid>             <version>1.4.5</version>         </dependency>     </dependencies>  </project> 

mailconfiguration.java

import org.springframework.beans.factory.annotation.value; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.propertysource; import org.springframework.mail.javamail.javamailsender; import org.springframework.mail.javamail.javamailsenderimpl;  import java.util.properties;  @configuration @propertysource("classpath:application.properties") public class mailconfiguration {     @value("${mail.host}")     private string host;     @value("${mail.username}")     private string username;     @value("${mail.password}")     private string password;     @value("${mail.smtp.auth}")     private string auth;     @value("${mail.smtp.port}")     private int port;     @value("${mail.smtp.starttls.enable}")     private string fallback; //    @value("${mail.smtp.ssl.enable}") //    private boolean ssl;      @bean     public javamailsender javamailsender()     {         javamailsenderimpl msender=new javamailsenderimpl();         properties mailproperties=new properties();         mailproperties.put("mail.smtp.auth",auth);         //mailproperties.put("mail.smtp.ssl.enable",ssl);         //mailproperties.put("spring.mail.properties.mail.smtp.socketfactory.class",socketclass);         mailproperties.put("mail.smtp.starttls.enable",fallback);         msender.setjavamailproperties(mailproperties);         msender.sethost(host);         msender.setport(port);         msender.setusername(username);         msender.setpassword(password);         return msender;     } } 

mailsender.java

import org.springframework.beans.factory.annotation.autowired; import org.springframework.mail.simplemailmessage; import org.springframework.mail.javamail.javamailsender; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller;  @restcontroller public class mailsender {     private final javamailsender javamailsender;      @autowired     mailsender(javamailsender javamailsender)     {         this.javamailsender = javamailsender;     }      @requestmapping("/send-mail")     simplemailmessage send()     {         simplemailmessage mailmessage = new simplemailmessage();         mailmessage.setto("abc@gmail.com");         //mailmessage.setreplyto("someone@localhost");         mailmessage.setfrom("xyz@gmail.com");         mailmessage.setsubject("lorem ipsum");         mailmessage.settext("lorem ipsum dolor sit amet [...]");         javamailsender.send(mailmessage);         return mailmessage;     } } 

i have created rest endpoint , after running apache server when access http://localhost:8080/send-mail http status 404 - /send-mail

you using wrong starttls port number gmail. 587


Comments