Skip to content

Commit eb6bf25

Browse files
committed
add initial version
1 parent 46e6e2b commit eb6bf25

25 files changed

Lines changed: 883 additions & 89 deletions

.github/workflows/main.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Publish package to GitHub Packages
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
version:
6+
description: "Version name to publish (eg: x.x.x)"
7+
type: string
8+
required: true
9+
10+
jobs:
11+
publish:
12+
timeout-minutes: 15
13+
runs-on: ubuntu-22.04
14+
permissions:
15+
contents: read
16+
packages: write
17+
18+
steps:
19+
- uses: actions/checkout@v3
20+
21+
- uses: aboutbits/github-actions-java/setup-and-update-dependencies@v1
22+
23+
- name: Set Version
24+
run: sed -i 's/<version>BUILD-SNAPSHOT<\/version>/<version>${{ github.event.inputs.version }}<\/version>/g' pom.xml
25+
26+
- name: Publish package
27+
run: mvn --batch-mode deploy
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
31+
tag:
32+
timeout-minutes: 5
33+
runs-on: ubuntu-22.04
34+
needs: publish
35+
steps:
36+
- name: Tag Deployment
37+
uses: aboutbits/github-actions-git/create-or-update-tag@v1
38+
with:
39+
tag-name: ${{ github.event.inputs.version }}
40+
user-name: 'AboutBits'
41+
user-email: 'info@aboutbits.it'

checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@
191191
<!-- Miscellaneous other checks. -->
192192
<!-- See https://checkstyle.org/config_misc.html -->
193193
<module name="ArrayTypeStyle"/>
194-
<module name="FinalParameters"/>
194+
<!--<module name="FinalParameters"/>-->
195195
<module name="TodoComment"/>
196196
<module name="UpperEll"/>
197197

license.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright About Bits GmbH
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

pom.xml

Lines changed: 75 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,81 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
<parent>
6-
<groupId>org.springframework.boot</groupId>
7-
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>3.1.1</version>
9-
<relativePath/> <!-- lookup parent from repository -->
10-
</parent>
11-
<groupId>it.aboutbits</groupId>
12-
<artifactId>springboot.emailservice</artifactId>
13-
<version>0.0.1-SNAPSHOT</version>
14-
<name>springboot.emailservice</name>
15-
<description>Spring Boot Email Service</description>
16-
<properties>
17-
<java.version>17</java.version>
18-
</properties>
19-
<dependencies>
20-
<dependency>
21-
<groupId>org.springframework.boot</groupId>
22-
<artifactId>spring-boot-starter-data-jpa</artifactId>
23-
</dependency>
24-
<dependency>
25-
<groupId>org.springframework.boot</groupId>
26-
<artifactId>spring-boot-starter-mail</artifactId>
27-
</dependency>
28-
<dependency>
29-
<groupId>org.liquibase</groupId>
30-
<artifactId>liquibase-core</artifactId>
31-
</dependency>
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
325

33-
<dependency>
34-
<groupId>org.postgresql</groupId>
35-
<artifactId>postgresql</artifactId>
36-
<scope>runtime</scope>
37-
</dependency>
38-
<dependency>
39-
<groupId>org.projectlombok</groupId>
40-
<artifactId>lombok</artifactId>
41-
<optional>true</optional>
42-
</dependency>
43-
<dependency>
44-
<groupId>org.springframework.boot</groupId>
45-
<artifactId>spring-boot-starter-test</artifactId>
46-
<scope>test</scope>
47-
</dependency>
48-
</dependencies>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>3.1.1</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
4912

50-
<build>
51-
<plugins>
52-
<plugin>
53-
<groupId>org.springframework.boot</groupId>
54-
<artifactId>spring-boot-maven-plugin</artifactId>
55-
<configuration>
56-
<excludes>
57-
<exclude>
58-
<groupId>org.projectlombok</groupId>
59-
<artifactId>lombok</artifactId>
60-
</exclude>
61-
</excludes>
62-
</configuration>
63-
</plugin>
64-
</plugins>
65-
</build>
13+
<groupId>it.aboutbits.springboot</groupId>
14+
<artifactId>emailservice</artifactId>
15+
<version>BUILD-SNAPSHOT</version>
16+
<description>Spring Boot Email Service</description>
6617

18+
<properties>
19+
<java.version>17</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-data-jpa</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-mail</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-validation</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.liquibase</groupId>
37+
<artifactId>liquibase-core</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.projectlombok</groupId>
41+
<artifactId>lombok</artifactId>
42+
<optional>true</optional>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-test</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-configuration-processor</artifactId>
52+
<optional>true</optional>
53+
</dependency>
54+
</dependencies>
55+
56+
<!--<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>com.github.spotbugs</groupId>
60+
<artifactId>spotbugs-maven-plugin</artifactId>
61+
<version>4.5.2.0</version>
62+
<dependencies>
63+
&lt;!&ndash; overwrite dependency on spotbugs if you want to specify the version of spotbugs &ndash;&gt;
64+
<dependency>
65+
<groupId>com.github.spotbugs</groupId>
66+
<artifactId>spotbugs</artifactId>
67+
<version>4.5.3</version>
68+
</dependency>
69+
</dependencies>
70+
</plugin>
71+
</plugins>
72+
</build>-->
73+
74+
<distributionManagement>
75+
<repository>
76+
<id>github</id>
77+
<name>GitHub Packages</name>
78+
<url>https://maven.pkg.github.com/aboutbits/spring-boot-sendEmailCommand-service</url>
79+
</repository>
80+
</distributionManagement>
6781
</project>

readme.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Spring Boot EMail Service
2+
3+
A reusable mailer service to send emails.
4+
5+
## Setup
6+
7+
Add the mailer service to the classpath by adding the following maven dependeny:
8+
9+
```xml
10+
11+
<dependency>
12+
<groupId>it.aboutbits.springboot</groupId>
13+
<artifactId>emailservice</artifactId>
14+
<version>x.x.x</version>
15+
</dependency>
16+
```
17+
18+
If you want to use attachments, you will have to create a bean implementing this interface: [AttachmentDataSource.java](src%2Fmain%2Fjava%2Fit%2Faboutbits%2Fspringboot%2Femailservice%2Flib%2FAttachmentDataSource.java)
19+
This step is optional.
20+
21+
## Usage
22+
23+
Use the `ManageEmail` service to schedule sending an email.
24+
25+
### Example
26+
27+
```java
28+
// @formatter:off
29+
public void sendMail(final String to,final String subject,final String htmlBody,final String plainTextBody) {
30+
manageEmail.schedule(
31+
EmailParameter.builder()
32+
.scheduleAt(OffsetDateTime.now())
33+
.email(EmailParameter.Email.builder()
34+
.fromName(fromName)
35+
.fromAddress(fromAddress)
36+
.recipient(to)
37+
.subject(subject)
38+
.textBody(plainTextBody)
39+
.htmlBody(htmlBody)
40+
.build())
41+
.build()
42+
);
43+
}
44+
// @formatter:on
45+
```
46+
47+
To read email datasets from the database use this class: [QueryEmail.java](src%2Fmain%2Fjava%2Fit%2Faboutbits%2Fspringboot%2Femailservice%2Flib%2Fapplication%2FQueryEmail.java)
48+
49+
If you want to receive a report after each run of the scheduler, create a Bean implementing [EmailSchedulerCallback.java](src%2Fmain%2Fjava%2Fit%2Faboutbits%2Fspringboot%2Femailservice%2Flib%2FEmailSchedulerCallback.java)
50+
51+
## Configuration
52+
53+
You will have to enable `EntityScanning` for the package as well as your main package. You must also add it to the base packages for Spring to scan.
54+
55+
```java
56+
@SpringBootApplication(scanBasePackages = {"the.main.package", "it.aboutbits.springboot.emailservice"})
57+
@EntityScan({"the.main.package", "it.aboutbits.springboot.emailservice"})
58+
```
59+
60+
Available configuration options are:
61+
62+
```yaml
63+
lib.emailservice.scheduling.enabled: true
64+
lib.emailservice.scheduling.interval: 30000
65+
```
66+
67+
## Building and releasing a new version:
68+
69+
To create a new version of this package and push it to the maven registry, you will have to use the GitHub actions workflow and manually trigger it.
70+
71+
## Information
72+
73+
About Bits is a company based in South Tyrol, Italy. You can find more information about us on [our website](https://aboutbits.it).
74+
75+
### Support
76+
77+
For support, please contact [info@aboutbits.it](mailto:info@aboutbits.it).
78+
79+
### Credits
80+
81+
- [All Contributors](../../contributors)
82+
83+
### License
84+
85+
The MIT License (MIT). Please see the [license file](license.md) for more information.

src/main/java/it/aboutbits/springboot/emailservice/Application.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package it.aboutbits.springboot.emailservice;
2+
3+
import it.aboutbits.springboot.emailservice.lib.AttachmentDataSource;
4+
import it.aboutbits.springboot.emailservice.lib.EmailSchedulerCallback;
5+
import it.aboutbits.springboot.emailservice.lib.application.ManageEmail;
6+
import it.aboutbits.springboot.emailservice.lib.application.QueryEmail;
7+
import it.aboutbits.springboot.emailservice.lib.application.SendScheduledEmails;
8+
import it.aboutbits.springboot.emailservice.lib.application.UnavailableAttachmentDataSource;
9+
import it.aboutbits.springboot.emailservice.lib.jpa.EmailRepository;
10+
import jakarta.persistence.EntityManager;
11+
import liquibase.integration.spring.SpringLiquibase;
12+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
13+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
14+
import org.springframework.context.annotation.Bean;
15+
import org.springframework.context.annotation.Configuration;
16+
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
17+
import org.springframework.mail.javamail.JavaMailSender;
18+
19+
import javax.sql.DataSource;
20+
import java.util.List;
21+
22+
@Configuration
23+
public class EmailServiceAutoConfiguration {
24+
@Bean(name = "emailServiceLiquibase")
25+
public SpringLiquibase emailServiceLiquibase(DataSource dataSource) {
26+
var liquibase = new SpringLiquibase();
27+
liquibase.setDataSource(dataSource);
28+
liquibase.setChangeLog("classpath:/db/changelog/emailservice/main.xml");
29+
liquibase.setShouldRun(true);
30+
return liquibase;
31+
}
32+
33+
@Bean
34+
public EmailRepository emailRepository(EntityManager entityManager) {
35+
var factory = new JpaRepositoryFactory(entityManager);
36+
return factory.getRepository(EmailRepository.class);
37+
}
38+
39+
@Bean
40+
public QueryEmail queryEmail(EmailRepository emailRepository) {
41+
return new QueryEmail(emailRepository);
42+
}
43+
44+
@Bean
45+
public ManageEmail manageEmail(EmailRepository emailRepository, JavaMailSender javaMailSender, AttachmentDataSource attachmentDataSource) {
46+
return new ManageEmail(emailRepository, javaMailSender, attachmentDataSource);
47+
}
48+
49+
@Bean
50+
@ConditionalOnProperty(value = "lib.emailservice.scheduling.enabled", matchIfMissing = true)
51+
public SendScheduledEmails sendScheduledEmails(QueryEmail queryEmail, ManageEmail manageEmail, List<EmailSchedulerCallback> callbacks) {
52+
return new SendScheduledEmails(queryEmail, manageEmail, callbacks);
53+
}
54+
55+
@Bean
56+
@ConditionalOnMissingBean(AttachmentDataSource.class)
57+
public AttachmentDataSource attachmentDataSource() {
58+
return new UnavailableAttachmentDataSource();
59+
}
60+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package it.aboutbits.springboot.emailservice.lib;
2+
3+
import java.io.InputStream;
4+
5+
public interface AttachmentDataSource {
6+
InputStream getAttachmentPayload(String reference);
7+
8+
void releaseAttachment(String reference);
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package it.aboutbits.springboot.emailservice.lib;
2+
3+
public interface EmailSchedulerCallback {
4+
void report(Report report);
5+
6+
record Report(
7+
int totalAttempted,
8+
int sent,
9+
int errors
10+
) {
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package it.aboutbits.springboot.emailservice.lib;
2+
3+
public enum EmailState {
4+
PENDING,
5+
SENT,
6+
ERROR
7+
}

0 commit comments

Comments
 (0)