Spring Boot Email
Spring Boot Email
RAGHU
Spring Email API is a simplified email service which can be implemented and
integrated with any spring application easily.
By using Java mail API (given by Sun Microsystem) coding and setup is lengthy,
it is simplified with POJI-POJO given below:
Spring Email API supports MIME Type Email Sending (Multipurpose Internet
Mail Extension). It means “Any kind of file as attachment”,
EX: Video, Audio, Text, Document, Images etc…
I.e shown as :
Page 1 of 9
Mr. RAGHU
Page 2 of 9
Mr. RAGHU
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
Page 3 of 9
Mr. RAGHU
</dependency>
MailSender class:
package com.app.raghu.service;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
@Component
public class AppMailSender {
@Autowired
private JavaMailSender mailsender;
@Value("${spring.mail.username}")
private String from;
Page 4 of 9
Mr. RAGHU
// 3. Compose Message
helper.setTo(to);
helper.setFrom("[email protected]");
helper.setSubject(sub);
helper.setText(text);
helper.addAttachment(file.getFilename(), file);
// 4. Send Email
mailsender.send(message);
status = true;
} catch (Exception e) {
status = false;
e.printStackTrace();
}
return status;
}
}
Test class:
package com.app.raghu.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Component;
import com.app.raghu.service.AppMailSender;
@Component
public class TestMail implements CommandLineRunner {
@Autowired
Page 5 of 9
Mr. RAGHU
application.properties
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=********@gmail.com
spring.mail.password=************
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
@Component
public class AppMailSender {
Page 6 of 9
Mr. RAGHU
@Autowired
private JavaMailSender sender;
@Value("${spring.mail.username}")
private String from;
try {
//2. Fill details
//here 2nd params indicates attachment exist or not?
MimeMessageHelper helper = new
MimeMessageHelper(message, files!=null && files.length>0);
helper.setTo(to);
if(cc!=null)
helper.setCc(cc);
if(bcc!=null)
helper.setBcc(bcc);
helper.setFrom(from);
helper.setSubject(subject);
//helper.setText(text);
helper.setText(text,true);
Page 7 of 9
Mr. RAGHU
e.printStackTrace();
}
return sent;
}
}
Test class:
package com.app.raghu.runner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Component;
import com.app.raghu.service.AppMailSender;
@Component
public class TestEmailRunner implements CommandLineRunner {
@Autowired
private AppMailSender sender;
Page 8 of 9
Mr. RAGHU
file1,file2
});
if(sent)
System.out.println("EMAIL IS SENT");
else
System.out.println("SENDING FAILED");
}
Page 9 of 9