|
| 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