ACSSendMailConfiguration.java

  1. package com.sintia.ffl.admin.optique.catalogue.util.mail;

  2. import com.azure.communication.email.EmailClient;
  3. import com.azure.communication.email.EmailClientBuilder;
  4. import com.azure.core.http.HttpClient;
  5. import com.azure.core.http.ProxyOptions;
  6. import com.azure.core.util.HttpClientOptions;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.context.annotation.Profile;
  12. import org.springframework.util.StringUtils;

  13. import java.net.InetSocketAddress;

  14. @Configuration
  15. @Profile("!smtp")
  16. @Slf4j
  17. public class ACSSendMailConfiguration {

  18.     @Value("${ffl.mail.acs.resource-name:}")
  19.     private String acsResourceName;

  20.     @Value("${ffl.mail.acs.access-key:}")
  21.     private String acsAccessKey;

  22.     @Value("${ffl.mail.acs.proxyHost:}")
  23.     private String httpsProxyHost;

  24.     @Value("${ffl.mail.acs.proxyPort:}")
  25.     private Integer httpsProxyPort;

  26.     @Bean
  27.     public EmailClient emailClient(){

  28.         if(org.apache.commons.lang3.StringUtils.isBlank(this.acsResourceName)){
  29.             throw new IllegalStateException("La propriété ffl.mail.acs.resource-name n'est pas définie.");
  30.         }
  31.         if(org.apache.commons.lang3.StringUtils.isBlank(this.acsAccessKey)){
  32.             throw new IllegalStateException("La propriété ffl.mail.acs.access-key n'est pas définie.");
  33.         }

  34.         String connectionString = String.format(
  35.                 "endpoint=https://%1$s.communication.azure.com/;accesskey=%2$s",
  36.                 this.acsResourceName,
  37.                 this.acsAccessKey
  38.         );

  39.         HttpClientOptions httpClientOptions = new HttpClientOptions();

  40.         ProxyOptions proxyOptions = null;
  41.         if(StringUtils.hasText(this.httpsProxyHost) && this.httpsProxyPort != null) {
  42.             proxyOptions = new ProxyOptions(
  43.                     ProxyOptions.Type.HTTP,
  44.                     new InetSocketAddress(this.httpsProxyHost, this.httpsProxyPort)
  45.             );
  46.         } else {
  47.             log.info("Informations concernant le proxy HTTPS indéfinies ou incomplète, l'envoi de mails via Azure Communication Services n'utilisera pas de proxy.");
  48.         }

  49.         httpClientOptions.setProxyOptions(proxyOptions);
  50.         HttpClient httpClient = HttpClient.createDefault(httpClientOptions);

  51.         return new EmailClientBuilder()
  52.                 .connectionString(connectionString)
  53.                 .httpClient(httpClient)
  54.                 .buildClient();
  55.     }


  56. }