Skip to content

Commit 2e146f5

Browse files
committed
refactor: extract interface MailService
1 parent faff9ab commit 2e146f5

File tree

2 files changed

+142
-100
lines changed

2 files changed

+142
-100
lines changed

src/main/java/de/rwth/idsg/steve/service/MailService.java

Lines changed: 6 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -18,115 +18,21 @@
1818
*/
1919
package de.rwth.idsg.steve.service;
2020

21-
import com.google.common.base.Strings;
22-
import de.rwth.idsg.steve.SteveException;
23-
import de.rwth.idsg.steve.config.DelegatingTaskExecutor;
24-
import de.rwth.idsg.steve.repository.SettingsRepository;
2521
import de.rwth.idsg.steve.repository.dto.MailSettings;
26-
import lombok.extern.slf4j.Slf4j;
27-
import org.springframework.beans.factory.annotation.Autowired;
28-
import org.springframework.stereotype.Service;
2922

30-
import jakarta.mail.Authenticator;
31-
import jakarta.mail.Message;
3223
import jakarta.mail.MessagingException;
33-
import jakarta.mail.PasswordAuthentication;
34-
import jakarta.mail.Session;
35-
import jakarta.mail.Transport;
36-
import jakarta.mail.internet.InternetAddress;
37-
import jakarta.mail.internet.MimeMessage;
38-
39-
import java.util.Properties;
4024

4125
/**
4226
* @author Sevket Goekay <[email protected]>
43-
* @since 24.01.2016
27+
* @since 11.03.2025
4428
*/
45-
@Slf4j
46-
@Service
47-
public class MailService {
48-
49-
@Autowired private SettingsRepository settingsRepository;
50-
@Autowired private DelegatingTaskExecutor asyncTaskExecutor;
51-
52-
public MailSettings getSettings() {
53-
return settingsRepository.getMailSettings();
54-
}
55-
56-
public void sendTestMail() {
57-
try {
58-
send("Test", "Test");
59-
} catch (MessagingException e) {
60-
throw new SteveException("Failed to send mail", e);
61-
}
62-
}
63-
64-
public void sendAsync(String subject, String body) {
65-
asyncTaskExecutor.execute(() -> {
66-
try {
67-
send(subject, body);
68-
} catch (MessagingException e) {
69-
log.error("Failed to send mail", e);
70-
}
71-
});
72-
}
73-
74-
public void send(String subject, String body) throws MessagingException {
75-
MailSettings settings = getSettings();
76-
Session session = createSession(getSettings());
77-
78-
Message mail = new MimeMessage(session);
79-
mail.setSubject("[SteVe] " + subject);
80-
mail.setContent(body, "text/plain");
81-
mail.setFrom(new InternetAddress(settings.getFrom()));
82-
83-
for (String rep : settings.getRecipients()) {
84-
mail.addRecipient(Message.RecipientType.TO, new InternetAddress(rep));
85-
}
86-
87-
try (Transport transport = session.getTransport()) {
88-
transport.connect();
89-
transport.sendMessage(mail, mail.getAllRecipients());
90-
}
91-
}
92-
93-
// -------------------------------------------------------------------------
94-
// Private helpers
95-
// -------------------------------------------------------------------------
96-
97-
private static Session createSession(MailSettings settings) {
98-
Properties props = new Properties();
99-
String protocol = settings.getProtocol();
100-
101-
props.setProperty("mail.host", "" + settings.getHost());
102-
props.setProperty("mail.transport.protocol", "" + protocol);
103-
props.setProperty("mail." + protocol + ".port", "" + settings.getPort());
104-
105-
if (settings.getPort() == 465) {
106-
props.setProperty("mail." + protocol + ".ssl.enable", "" + true);
107-
108-
} else if (settings.getPort() == 587) {
109-
props.setProperty("mail." + protocol + ".starttls.enable", "" + true);
110-
}
29+
public interface MailService {
11130

112-
boolean isUserSet = !Strings.isNullOrEmpty(settings.getUsername());
113-
boolean isPassSet = !Strings.isNullOrEmpty(settings.getPassword());
31+
MailSettings getSettings();
11432

115-
if (isUserSet && isPassSet) {
116-
props.setProperty("mail." + protocol + ".auth", "" + true);
117-
return Session.getInstance(props, getAuth(settings));
33+
void sendTestMail();
11834

119-
} else {
120-
return Session.getInstance(props);
121-
}
122-
}
35+
void sendAsync(String subject, String body);
12336

124-
private static Authenticator getAuth(MailSettings settings) {
125-
return new Authenticator() {
126-
@Override
127-
protected PasswordAuthentication getPasswordAuthentication() {
128-
return new PasswordAuthentication(settings.getUsername(), settings.getPassword());
129-
}
130-
};
131-
}
37+
void send(String subject, String body) throws MessagingException;
13238
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* SteVe - SteckdosenVerwaltung - https://github.com/steve-community/steve
3+
* Copyright (C) 2013-2025 SteVe Community Team
4+
* All Rights Reserved.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package de.rwth.idsg.steve.service;
20+
21+
import jakarta.mail.Authenticator;
22+
import jakarta.mail.Message;
23+
import jakarta.mail.MessagingException;
24+
import jakarta.mail.PasswordAuthentication;
25+
import jakarta.mail.Session;
26+
import jakarta.mail.Transport;
27+
import jakarta.mail.internet.InternetAddress;
28+
import jakarta.mail.internet.MimeMessage;
29+
30+
import java.util.Properties;
31+
32+
import com.google.common.base.Strings;
33+
import de.rwth.idsg.steve.SteveException;
34+
import de.rwth.idsg.steve.config.DelegatingTaskExecutor;
35+
import de.rwth.idsg.steve.repository.SettingsRepository;
36+
import de.rwth.idsg.steve.repository.dto.MailSettings;
37+
import lombok.extern.slf4j.Slf4j;
38+
import org.springframework.beans.factory.annotation.Autowired;
39+
import org.springframework.stereotype.Service;
40+
41+
/**
42+
* @author Sevket Goekay <[email protected]>
43+
* @since 24.01.2016
44+
*/
45+
@Slf4j
46+
@Service
47+
public class MailServiceDefault implements MailService {
48+
49+
@Autowired private SettingsRepository settingsRepository;
50+
@Autowired private DelegatingTaskExecutor asyncTaskExecutor;
51+
52+
@Override
53+
public MailSettings getSettings() {
54+
return settingsRepository.getMailSettings();
55+
}
56+
57+
@Override
58+
public void sendTestMail() {
59+
try {
60+
send("Test", "Test");
61+
} catch (MessagingException e) {
62+
throw new SteveException("Failed to send mail", e);
63+
}
64+
}
65+
66+
@Override
67+
public void sendAsync(String subject, String body) {
68+
asyncTaskExecutor.execute(() -> {
69+
try {
70+
send(subject, body);
71+
} catch (MessagingException e) {
72+
log.error("Failed to send mail", e);
73+
}
74+
});
75+
}
76+
77+
@Override
78+
public void send(String subject, String body) throws MessagingException {
79+
MailSettings settings = getSettings();
80+
Session session = createSession(settings);
81+
82+
Message mail = new MimeMessage(session);
83+
mail.setSubject("[SteVe] " + subject);
84+
mail.setContent(body, "text/plain");
85+
mail.setFrom(new InternetAddress(settings.getFrom()));
86+
87+
for (String rep : settings.getRecipients()) {
88+
mail.addRecipient(Message.RecipientType.TO, new InternetAddress(rep));
89+
}
90+
91+
try (Transport transport = session.getTransport()) {
92+
transport.connect();
93+
transport.sendMessage(mail, mail.getAllRecipients());
94+
}
95+
}
96+
97+
// -------------------------------------------------------------------------
98+
// Private helpers
99+
// -------------------------------------------------------------------------
100+
101+
private static Session createSession(MailSettings settings) {
102+
Properties props = new Properties();
103+
String protocol = settings.getProtocol();
104+
105+
props.setProperty("mail.host", "" + settings.getHost());
106+
props.setProperty("mail.transport.protocol", "" + protocol);
107+
props.setProperty("mail." + protocol + ".port", "" + settings.getPort());
108+
109+
if (settings.getPort() == 465) {
110+
props.setProperty("mail." + protocol + ".ssl.enable", "" + true);
111+
112+
} else if (settings.getPort() == 587) {
113+
props.setProperty("mail." + protocol + ".starttls.enable", "" + true);
114+
}
115+
116+
boolean isUserSet = !Strings.isNullOrEmpty(settings.getUsername());
117+
boolean isPassSet = !Strings.isNullOrEmpty(settings.getPassword());
118+
119+
if (isUserSet && isPassSet) {
120+
props.setProperty("mail." + protocol + ".auth", "" + true);
121+
return Session.getInstance(props, getAuth(settings));
122+
123+
} else {
124+
return Session.getInstance(props);
125+
}
126+
}
127+
128+
private static Authenticator getAuth(MailSettings settings) {
129+
return new Authenticator() {
130+
@Override
131+
protected PasswordAuthentication getPasswordAuthentication() {
132+
return new PasswordAuthentication(settings.getUsername(), settings.getPassword());
133+
}
134+
};
135+
}
136+
}

0 commit comments

Comments
 (0)