spring-bootsendinbluebrevo

Sendinblue Issue after upgrading to SringBoot 3.2.1


After upgrading to spring boot from v3.1.2 to v3.2.1 I got an issue with my sendinblue api

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailSenderProvider': Invocation of init method failed

...

Caused by: java.lang.NoSuchFieldError: Class okio.Options does not have member field 'okio.Options$Companion Companion'
    at okhttp3.internal.Util.<clinit>(Util.kt:70) ~[okhttp-4.12.0.jar:na]
    at okhttp3.OkHttpClient.<clinit>(OkHttpClient.kt:1073) ~[okhttp-4.12.0.jar:na]
    at sendinblue.ApiClient.<init>(ApiClient.java:80) ~[sib-api-v3-sdk-7.0.0.jar:na]
    at sendinblue.Configuration.<clinit>(Configuration.java:18) ~[sib-api-v3-sdk-7.0.0.jar:na]

Here my code

        <dependency>
            <groupId>com.sendinblue</groupId>
            <artifactId>sib-api-v3-sdk</artifactId>
            <version>7.0.0</version>
            <scope>compile</scope>
        </dependency>

// MailSenderProvider.java

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sendinblue.ApiClient;
import sendinblue.auth.ApiKeyAuth;
import sibApi.ContactsApi;
import sibApi.TransactionalEmailsApi;

@Configuration
@RequiredArgsConstructor
public class MailSenderProvider {

    private final MailConfiguration mailConfiguration;

    @PostConstruct
    private void initialize() {
        ApiClient defaultClient = sendinblue.Configuration.getDefaultApiClient();
        ApiKeyAuth apiKeyAuth = (ApiKeyAuth) defaultClient.getAuthentication("api-key");
        apiKeyAuth.setApiKey(mailConfiguration.getApiKey());
    }

    @Bean
    public TransactionalEmailsApi transactionalEmailsApi() {
        return new TransactionalEmailsApi();
    }

    @Bean
    public ContactsApi contactsApi() {
        return new ContactsApi();
    }
}

// MailService

import sendinblue.ApiException;
import sibApi.TransactionalEmailsApi;
import sibModel.SendSmtpEmail;
import sibModel.SendSmtpEmailSender;
import sibModel.SendSmtpEmailTo;

@Service
@RequiredArgsConstructor
@Slf4j
@EnableAsync
public class MailServiceImpl implements MailService {


    private final MailConfiguration mailConfiguration;
    private final TransactionalEmailsApi transactionalEmailsApi;

    private SendSmtpEmailSender noReplyEmail;
    private SendSmtpEmailTo contactEmail;

    @PostConstruct
    private void init() {
        noReplyEmail = new SendSmtpEmailSender().email(mailConfiguration.getEmail().getNoreply()).name(mailConfiguration.getName());
        contactEmail = new SendSmtpEmailTo().email(mailConfiguration.getEmail().getContact()).name(mailConfiguration.getName());
    }

    @Override
    @Async
    public CompletableFuture<Void> sendRegistrationEMail(String name, String email, String registrationToken, String url) {
        Properties headers = new Properties();
        headers.setProperty("Content-Type", "text/html; charset=UTF-8");

        Properties params = new Properties();
        params.setProperty("token", registrationToken);
        params.setProperty("name", name);
        params.setProperty("url", url + "/account/confirm?token=" + registrationToken);

        SendSmtpEmail sendSmtpEmail = new SendSmtpEmail()
                .sender(noReplyEmail)
                .addToItem(new SendSmtpEmailTo().email(email))
                .subject("Votre Compte ...")
                .headers(headers)
                .params(params)
                .templateId(mailConfiguration.getTemplate().getRegistrationMail());
        try {
            transactionalEmailsApi.sendTransacEmail(sendSmtpEmail);
        } catch (ApiException e) {
            log.error(e.getResponseBody());
        }
        return new CompletableFuture<>();
    }

Solution

  • It was really a dependency conflict, to fix it I added this in my pom.xml

    
    <dependency>
       <groupId>com.sendinblue</groupId>
       <artifactId>sib-api-v3-sdk</artifactId>
       <version>7.0.0</version>
       <scope>compile</scope>
           <exclusions>
              <exclusion>
                  <groupId>com.squareup.okio</groupId>
                  <artifactId>okio</artifactId>
              </exclusion>
           </exclusions>
    </dependency>
    <dependency>
        <groupId>com.squareup.okio</groupId>
        <artifactId>okio</artifactId>
        <version>3.6.0</version> <!-- a version supported by Sendinblue SDK -->
    </dependency>