这是我第一次尝试使用spring-boot-starter-mail发送邮件。我没有收到任何错误,但在发送邮件时我看不到任何邮件到我的TO ID。
application.properties
spring.mail.host = smtp.mandrillapp.com
spring.mai.username = lending@vit.in
spring.mail.password = ********
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.starttls.enable = true
spring.mail.properties.mail.smtp.socketFactory.fallback = false
我的申请类
@SpringBootApplication
@ComponentScan(basePackages = {"com.mail"})
public class MailServiceApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MailServiceApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MailServiceApplication.class, args);
}
@Bean
public ServletRegistrationBean jerseyServlet(){
ServletRegistrationBean register = new ServletRegistrationBean(new ServletContainer(),"/*");
register.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyInitialization.class.getName());
return register;
}
}
JerseyInitialization.class
@Component
public class JerseyInitialization extends ResourceConfig{
public JerseyInitialization(){
super();
this.packages("com.mail.interfaces");
System.out.println("111");
//register(GenericFilter.class);
// property(ServerProperties.MONITORING_STATISTICS_ENABLED, true);
}
}
控制器类:
@RestController
@Path("/mail")
public class MailResource {
@Autowired
MailSendInter mailInter;
@GET
@Path("/send")
public String sendMail(){
System.out.println("inside the resource Class");
boolean result = mailInter.send("aravind11081990@gmail.com", "Welcome Mail","Wlcome to the lending system");
if(result){
return "ok";
}else{
return "failure";
}
}
}
接口类:
@Service
public interface MailSendInter {
public boolean send(String to, String subject, String body);
}
ImplementationClass:
@Component
public class MailSendImplementation implements MailSendInter{
@Autowired
private JavaMailSender javaMailSender;
@Override
public boolean send(String to, String subject, String body) {
// TODO Auto-generated method stub
boolean result = false;
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try{
MimeMessageHelper helper ;
helper = new MimeMessageHelper(mimeMessage, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(body, true);
javaMailSender.send(mimeMessage);
result = true;
}catch(MessagingException mex){
result = false;
System.err.println("Expection"+mex);
}
return result;
}
}
的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>com.mail</groupId>
<artifactId>MailService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MailService</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我正在使用Mandrill发送邮件
URL:https://www.mandrill.com 用户名:lending@vit.in 密码:********
主持人:smtp.mandrillapp.com 港口:587 SMTP用户名:lending@vit.in SMTP密码:********
有人可以帮助找出错误