love.keeping.starter.web.config.MailAutoConfiguration Maven / Gradle / Ivy
The newest version!
package love.keeping.starter.web.config;
import cn.hutool.extra.mail.MailAccount;
import love.keeping.starter.web.config.properties.MailProperties;
import love.keeping.starter.web.impl.MailServiceImpl;
import love.keeping.starter.web.service.MailService;
import love.keeping.starter.web.service.SysParameterService;
import love.keeping.starter.web.utils.JsonUtil;
import com.sun.mail.util.MailSSLSocketFactory;
import java.security.GeneralSecurityException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class MailAutoConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnMissingBean(MailService.class)
public MailService getMailService(SysParameterService sysParameterService)
throws GeneralSecurityException {
String configStr = sysParameterService.findRequiredByKey("mail");
MailProperties properties = JsonUtil.parseObject(configStr, MailProperties.class);
MailAccount account = new MailAccount();
account.setHost(properties.getHost());
account.setPort(properties.getPort());
account.setUser(properties.getUser());
account.setPass(properties.getPass());
account.setFrom(properties.getFrom());
account.setSslEnable(properties.getSslEnable());
account.setTimeout(properties.getTimeOut());
account.setConnectionTimeout(properties.getConnectTimeOut());
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
account.setCustomProperty("mail.smtp.ssl.socketFactory", sf);
return new MailServiceImpl(account);
}
}