Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions cms-api/src/main/java/com/condation/cms/api/mail/MailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.condation.cms.api.mail;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 - 2025 CondationCMS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

/**
*
* @author thmar
*/
public interface MailService {
void sendText (String account, Message message);

void sendHtml (String account, Message message);

default void sendText (Message message) {
sendText("default", message);
}

default void sendHtml (Message message) {
sendHtml("default", message);
}
}
38 changes: 38 additions & 0 deletions cms-api/src/main/java/com/condation/cms/api/mail/Message.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.condation.cms.api.mail;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 - 2025 CondationCMS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import java.util.List;

/**
*
* @author thmar
*/
public record Message(String from, List<Recipient> to, String subject, String message) {

public Message (String from, Recipient to, String subject, String message) {
this(from, List.of(to), subject, message);
}

public static record Recipient (String name, String mailAddress){}
}
17 changes: 17 additions & 0 deletions cms-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@
<artifactId>jtoml-serializer-gson</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>8.12.6</version>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>2.1.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail-junit5</artifactId>
<version>2.1.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.condation.cms.core.mail;

/*-
* #%L
* cms-core
* %%
* Copyright (C) 2023 - 2025 CondationCMS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.condation.cms.api.db.DB;
import com.condation.cms.api.mail.MailService;
import com.condation.cms.api.mail.Message;
import java.nio.file.Files;
import org.simplejavamail.api.email.Recipient;
import org.simplejavamail.api.mailer.Mailer;
import org.simplejavamail.email.EmailBuilder;
import org.simplejavamail.mailer.MailerBuilder;

/**
*
* @author thmar
*/
public class DefaultMailService implements MailService {

private final DB db;

private MailConfig config;

public DefaultMailService(DB db) {
this.db = db;

init();
}

private void init () {
var mailConfig = db.getFileSystem().resolve("config/mail.yaml");
if (Files.exists(mailConfig)) {
config = MailConfigLoader.load(mailConfig);
}
}

@Override
public void sendText(String account, Message message) {
var acc = getAccount(account);

var mail = EmailBuilder.startingBlank()
.appendText(message.message())
.withSubject(message.subject())
.from(message.from(), acc.getFromMail())
.to(message.to().stream().map(rec -> new Recipient(rec.name(), rec.mailAddress(), null)).toList());

buildMailer(acc).sendMail(mail.buildEmail());
}

@Override
public void sendHtml(String account, Message message) {
var acc = getAccount(account);

var mail = EmailBuilder.startingBlank()
.appendTextHTML(message.message())
.withSubject(message.subject())
.from(message.from(), acc.getFromMail())
.to(message.to().stream().map(rec -> new Recipient(rec.name(), rec.mailAddress(), null)).toList());

buildMailer(acc).sendMail(mail.buildEmail());
}

private MailConfig.Account getAccount(String account) throws RuntimeException {
var acc = config.getAccount(account);
if (acc.isEmpty()) {
throw new RuntimeException("unknown account");
}
return acc.get();
}


private Mailer buildMailer (MailConfig.Account account) {
return MailerBuilder.withSMTPServer(
account.getHost(),
account.getPort(),
account.getUsername(),
account.getPassword()
).buildMailer();
}
}
54 changes: 54 additions & 0 deletions cms-core/src/main/java/com/condation/cms/core/mail/MailConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.condation.cms.core.mail;

/*-
* #%L
* cms-core
* %%
* Copyright (C) 2023 - 2025 CondationCMS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import java.util.List;
import java.util.Optional;
import lombok.Data;

/**
*
* @author thmar
*/
@Data
public class MailConfig {

private List<Account> accounts;

public Optional<Account> getAccount (String name) {
if (accounts == null || accounts.isEmpty()) {
return Optional.empty();
}
return accounts.stream().filter(acc -> name.equals(acc.getName())).findFirst();
}

@Data
public static class Account {
private String name;
private String fromMail;
private String host;
private int port;
private String username;
private String password;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.condation.cms.core.mail;

/*-
* #%L
* cms-core
* %%
* Copyright (C) 2023 - 2025 CondationCMS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

/**
*
* @author thorstenmarx
*/
public class MailConfigLoader {

public static MailConfig load(Path configFile) {
try {
try (var configByteBuffer = Files.newBufferedReader(configFile, StandardCharsets.UTF_8)) {
Yaml yaml = new Yaml(new Constructor(MailConfig.class, new LoaderOptions()));
return yaml.load(configByteBuffer);
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
Loading