spring boot mail 发送 设置代理服务器

背景:

公司使用spring cloud 微服务  docker + Rancher + drone + maven 自动化部署。

网络环境 内网环境内部微服务必须通过代理服务器对外访问。邮件发送 必须通过代理服务器进行发送。

spring boot 封装了 JavaMail的类库 。

Spring Framework提供了一个使用JavaMailSender接口发送电子邮件的简单抽象 ,Spring Boot为它提供了自动配置以及启动器模块。
JavaMailSenderImpl为JavaMailSender接口的实现类实现发送。MailProperties为配置信息。
//源代码接口JavaMailSender 继承 MailSender 进行功能增强操作
public interface JavaMailSender extends MailSender {

    MimeMessage createMimeMessage();

    MimeMessage createMimeMessage(InputStream var1) throws MailException;

    void send(MimeMessage var1) throws MailException;

    void send(MimeMessage... var1) throws MailException;

    void send(MimeMessagePreparator var1) throws MailException;

    void send(MimeMessagePreparator... var1) throws MailException;
}

实现类代码 

//发送邮件的实现类源码
public class JavaMailSenderImpl implements JavaMailSender {
    public static final String DEFAULT_PROTOCOL = "smtp";
    public static final int DEFAULT_PORT = -1;
    private static final String HEADER_MESSAGE_ID = "Message-ID";
    //这个配置就是对应的JavaMail的资源信息
    private Properties javaMailProperties = new Properties();
    private Session session;
    private String protocol;
    private String host;
    private int port = -1;
    private String username;
    private String password;
    private String defaultEncoding;
    private FileTypeMap defaultFileTypeMap;

    public JavaMailSenderImpl() {
        ConfigurableMimeFileTypeMap fileTypeMap = new ConfigurableMimeFileTypeMap();
        fileTypeMap.afterPropertiesSet();
        this.defaultFileTypeMap = fileTypeMap;
    }

    public void setJavaMailProperties(Properties javaMailProperties) {
        this.javaMailProperties = javaMailProperties;
        synchronized(this) {
            this.session = null;
        }
    }

    public Properties getJavaMailProperties() {
        return this.javaMailProperties;
    }

    public synchronized void setSession(Session session) {
        Assert.notNull(session, "Session must not be null");
        this.session = session;
    }
    /**省略
}

同样,在java mail的官方文档中也有提到,如果使用的是jdk 1.5以上和java.mail 1.4.5,可以通过javaMailProperties配置,更多配置项,参考 com.sun.mail.smtp package。

  
      
      
      
      
      
      
          
              
            true  
            25000  
            true  
            true  
            cmproxy.gmcc.net  
            8081  
            javax.net.ssl.SSLSocketFactory  
          
      
  

可以看到 javaMailProperties配置属性 就可以实现socks代理服务器发送邮件

 

所以在我们在spring boot 里面 可以直接这样去设置 

/**
 * @Author: xlr
 * @Date: Created in 下午11:31 2018/11/8
 */
@Configuration
@Import(MailProperties.class)
public class MailConfig {

    @Autowired
    MailProperties mailProperties;
    @Value( "${porxyHost}" )
    private String porxyHost;
    @Value( "${porxyPort}" )
    private String porxyPort;
    @Value( "${proxySet}" )
    private String proxySet;

    @Bean
    public JavaMailSender javaMailSender() {

        Properties properties = new Properties();
        properties.setProperty( "mail.smtp.socks.host", porxyHost );
        properties.setProperty( "mail.smtp.socks.port", porxyPort );
        properties.setProperty( "mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory" );
        properties.setProperty( "mail.smtp.auth", "true" );
        properties.setProperty( "mail.smtp.timeout", "25000" );
        properties.setProperty( "mail.smtp.ssl.enable", "true" );
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setJavaMailProperties( properties );
        mailSender.setHost( mailProperties.getHost() );
        mailSender.setPassword( mailProperties.getPassword() );
        mailSender.setUsername( mailProperties.getUsername() );
        mailSender.setProtocol( mailProperties.getProtocol() );
        return mailSender;
    }



}

设置邮件代理服务邮件发送。

你可能感兴趣的:(spring,boot)