package com.mailserver.config; import com.mailserver.delivery.SmtpDeliveryClient; import com.mailserver.smtp.MailRouter; import com.mailserver.smtp.SmtpServer; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import java.time.Duration; import java.util.Locale; /** SMTP server, outbound delivery client and the queue scheduler. */ @Configuration @EnableScheduling public class MailServerConfig { @Bean(initMethod = "start", destroyMethod = "stop") @ConditionalOnProperty(name = "mailserver.smtp.enabled", havingValue = "true", matchIfMissing = true) public SmtpServer smtpServer(MailRouter router, @Value("${mailserver.smtp.host:0.0.0.0}") String host, @Value("${mailserver.smtp.port:3025}") int port, @Value("${mailserver.smtp.hostname:localhost}") String hostname, @Value("${mailserver.smtp.max-message-bytes:26214400}") long maxBytes, @Value("${mailserver.smtp.auth-without-tls:false}") boolean authWithoutTls, @Value("${mailserver.smtp.idle-timeout:5m}") Duration idleTimeout) { return new SmtpServer(new SmtpServer.Settings(host, port, hostname, maxBytes, authWithoutTls, idleTimeout), router); } /** * mailserver.outbound.mode: off | mx | smarthost. For smarthost — host, port, username, password, * tls (starttls | ssl | none). */ @Bean public SmtpDeliveryClient smtpDeliveryClient(@Value("${mailserver.outbound.mode:off}") String mode, @Value("${mailserver.outbound.host:}") String host, @Value("${mailserver.outbound.port:587}") int port, @Value("${mailserver.outbound.username:}") String username, @Value("${mailserver.outbound.password:}") String password, @Value("${mailserver.outbound.tls:starttls}") String tls, @Value("${mailserver.smtp.hostname:localhost}") String hostname, @Value("${mailserver.outbound.timeout-ms:30000}") int timeoutMs) { SmtpDeliveryClient.Mode m = SmtpDeliveryClient.Mode.valueOf(mode.trim().toUpperCase(Locale.ROOT)); if (m == SmtpDeliveryClient.Mode.SMARTHOST && host.isBlank()) { throw new IllegalStateException("mailserver.outbound.mode=smarthost needs mailserver.outbound.host"); } return new SmtpDeliveryClient(new SmtpDeliveryClient.Settings(m, host, port, username, password, SmtpDeliveryClient.Tls.valueOf(tls.trim().toUpperCase(Locale.ROOT)), hostname, timeoutMs, 25)); } }