Skip to content

Commit 11a1d2a

Browse files
abburi03abburi03
abburi03
authored and
abburi03
committed
Common-Email Service
0 parents  commit 11a1d2a

File tree

11 files changed

+350
-0
lines changed

11 files changed

+350
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.iml
2+
.idea/*
3+
target/*

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This demo uses spring-boot and spring mail to send emails. For testing purpose I used greenmail.
2+
3+
To run this application, please visit src/main/resources/application.properties and change email.sender.username, email.sender.password to your credentials.
4+
5+
Running the EmailApplication.java will start this application in http://localhost:8080
6+
7+
For a simple test purpose i have added /testSendEmail to send out email. You can change the toAddress to see real email in SendEmailController.
8+
9+
Note : Gmail will not allow using its smtp from less secured apps. You might need to switch off that feature while testing to send email.
10+
In gmail, my account goto Sign-in & Security --> Connected apps & sites --> Allow less secure apps: turn it on

pom.xml

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<artifactId>common-email</artifactId>
9+
<groupId>email</groupId>
10+
<version>1.0</version>
11+
<packaging>jar</packaging>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-mail</artifactId>
17+
<version>1.5.3.RELEASE</version>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
<version>1.5.3.RELEASE</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-test</artifactId>
29+
<version>1.5.3.RELEASE</version>
30+
<scope>test</scope>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>com.icegreen</groupId>
35+
<artifactId>greenmail</artifactId>
36+
<version>1.5.5</version>
37+
</dependency>
38+
</dependencies>
39+
40+
<build>
41+
<resources>
42+
<resource>
43+
<directory>src/main/resources</directory>
44+
<filtering>true</filtering>
45+
<includes>
46+
<include>application.properties</include>
47+
</includes>
48+
</resource>
49+
</resources>
50+
</build>
51+
52+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.common.email;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Created by abburi on 6/18/17.
8+
*/
9+
10+
@SpringBootApplication
11+
public class EmailApplication {
12+
13+
public static void main(String[] args){
14+
SpringApplication.run(EmailApplication.class, args);
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.common.email;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.mail.javamail.JavaMailSender;
7+
import org.springframework.mail.javamail.JavaMailSenderImpl;
8+
9+
import java.util.Properties;
10+
11+
/**
12+
* Created by abburi on 6/10/17.
13+
*/
14+
15+
@Configuration
16+
public class EmailSenderConfig {
17+
18+
19+
@Value("${email.sender.host}")
20+
private String host;
21+
22+
@Value("${email.sender.port}")
23+
private String port;
24+
25+
@Value("${email.sender.username}")
26+
private String username;
27+
28+
@Value("${email.sender.password}")
29+
private String password;
30+
31+
@Value("${email.transport.protocol}")
32+
private String protocol;
33+
34+
@Value("${email.smtp.auth}")
35+
private String auth;
36+
37+
@Value("${email.smtp.starttls.enable}")
38+
private String ttlsEnable;
39+
40+
@Value("${email.debug}")
41+
private String debug;
42+
43+
44+
@Bean
45+
public JavaMailSender getJavaMailSender(){
46+
47+
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
48+
mailSender.setHost(host);
49+
mailSender.setPort(Integer.valueOf(port));
50+
51+
mailSender.setUsername(username);
52+
mailSender.setPassword(password);
53+
54+
mailSender.setJavaMailProperties(getMailSenderProperties());
55+
56+
return mailSender;
57+
}
58+
59+
private Properties getMailSenderProperties(){
60+
Properties props = new Properties();
61+
props.put("mail.transport.protocol", protocol);
62+
props.put("mail.smtp.auth", auth);
63+
props.put("mail.smtp.starttls.enable", ttlsEnable);
64+
props.put("mail.debug", debug);
65+
66+
return props;
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.common.email.controller;
2+
3+
import org.common.email.service.EmailService;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import javax.mail.MessagingException;
10+
11+
/**
12+
* Created by abburi on 6/18/17.
13+
*/
14+
15+
@RestController
16+
public class SendEmailController {
17+
18+
@Autowired
19+
private EmailService emailService;
20+
21+
@RequestMapping(value = "/testSendEmail" , method = RequestMethod.GET)
22+
public void sendEmail(){
23+
try {
24+
emailService.sendMail("[email protected]", "Test Subject", "TestMessage");
25+
} catch (MessagingException e) {
26+
e.printStackTrace();
27+
}
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.common.email.service;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.core.io.FileSystemResource;
6+
import org.springframework.mail.MailMessage;
7+
import org.springframework.mail.SimpleMailMessage;
8+
import org.springframework.mail.javamail.JavaMailSender;
9+
import org.springframework.mail.javamail.MimeMailMessage;
10+
import org.springframework.mail.javamail.MimeMessageHelper;
11+
import org.springframework.stereotype.Service;
12+
13+
import javax.mail.MessagingException;
14+
import javax.mail.internet.MimeMessage;
15+
import java.io.File;
16+
17+
/**
18+
* Created by abburi on 6/10/17.
19+
*/
20+
21+
@Service
22+
public class EmailService {
23+
24+
@Autowired
25+
private JavaMailSender javaMailSender;
26+
27+
@Value("${email.from.addres}")
28+
private String fromAddress;
29+
30+
public void sendMailMultipart(String toEmail, String subject, String message, File file) throws MessagingException {
31+
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
32+
33+
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
34+
helper.setFrom(fromAddress);
35+
helper.setTo(toEmail);
36+
helper.setSubject(subject);
37+
helper.setText(message);
38+
39+
if(file != null){
40+
helper.addAttachment(file.getName(), file);
41+
}
42+
javaMailSender.send(mimeMessage);
43+
}
44+
45+
public void sendMail(String toEmail, String subject, String message) throws MessagingException {
46+
sendMailMultipart(toEmail, subject, message, null);
47+
}
48+
49+
public void sendMail(String toEmail, String subject, String message, File file) throws MessagingException {
50+
sendMailMultipart(toEmail, subject, message, file);
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
email.sender.host=smtp.gmail.com
2+
email.sender.port=587
3+
email.sender.username=yourgmailemail
4+
email.sender.password=yourgmailpassword
5+
email.transport.protocol=smtp
6+
email.smtp.auth=true
7+
email.smtp.starttls.enable=true
8+
email.debug=false
9+
email.from.addres[email protected]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.common.email;
2+
3+
import com.icegreen.greenmail.util.GreenMail;
4+
import com.icegreen.greenmail.util.GreenMailUtil;
5+
import com.icegreen.greenmail.util.ServerSetupTest;
6+
import org.common.email.service.EmailService;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
14+
import javax.mail.BodyPart;
15+
import javax.mail.Message;
16+
import javax.mail.MessagingException;
17+
import javax.mail.Multipart;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertThat;
24+
import static org.junit.Assert.assertTrue;
25+
26+
/**
27+
* Created by abburi on 6/10/17.
28+
*/
29+
30+
@RunWith(SpringRunner.class)
31+
@SpringBootTest(classes = {EmailSenderConfig.class, EmailService.class})
32+
public class EmailServiceTest {
33+
34+
@Autowired
35+
private EmailService emailService;
36+
37+
private GreenMail testSmtp;
38+
39+
40+
@Test
41+
public void testEmail() throws MessagingException, IOException {
42+
testSmtp = new GreenMail(ServerSetupTest.SMTP);
43+
testSmtp.start();
44+
emailService.sendMail("[email protected]", "Test Subject", "Test mail");
45+
46+
testSmtp.waitForIncomingEmail(1);
47+
48+
Message[] messages = testSmtp.getReceivedMessages();
49+
Assert.assertTrue(messages.length == 1);
50+
51+
String message = messages[0].getSubject();
52+
53+
Multipart part = (Multipart) messages[0].getContent();
54+
55+
assertEquals(part.getCount(), 1);
56+
57+
assertEquals("Test Subject", message);
58+
String body = GreenMailUtil.getBody(messages[0]).replaceAll("=\r?\n", "");
59+
assertTrue(body.contains("Test mail"));
60+
61+
testSmtp.stop();
62+
63+
64+
}
65+
66+
@Test
67+
public void testEmailWithFile() throws MessagingException, IOException {
68+
testSmtp = new GreenMail(ServerSetupTest.SMTP);
69+
testSmtp.start();
70+
71+
72+
73+
File file = new File(this.getClass().getClassLoader().getResource("TestFile.txt").getFile());
74+
emailService.sendMail("[email protected]", "Test Subject", "Test mail", file);
75+
76+
testSmtp.waitForIncomingEmail(1);
77+
78+
Message[] messages = testSmtp.getReceivedMessages();
79+
Assert.assertTrue(messages.length == 1);
80+
81+
String message = messages[0].getSubject();
82+
assertEquals("Test Subject", message);
83+
String body = GreenMailUtil.getBody(messages[0]).replaceAll("=\r?\n", "");
84+
assertTrue(body.contains("Test mail"));
85+
86+
Multipart part = (Multipart) messages[0].getContent();
87+
88+
assertEquals(part.getCount(), 2);
89+
90+
BodyPart bodyPart = part.getBodyPart(1);
91+
assertEquals(bodyPart.getFileName(), "TestFile.txt");
92+
93+
assertEquals(bodyPart.getContent().toString(), "Testing file to be sent as email attachment");
94+
95+
testSmtp.stop();
96+
97+
}
98+
99+
}

src/test/resources/TestFile.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Testing file to be sent as email attachment
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
email.sender.host=localhost
2+
email.sender.port=3025
3+
email.sender.username=no
4+
email.sender.password=no
5+
email.transport.protocol=smtp
6+
email.smtp.auth=false
7+
email.smtp.starttls.enable=false
8+
email.debug=true
9+
email.from.addres[email protected]

0 commit comments

Comments
 (0)