Error while sending mail via script: IMAPProvider not found
Asked by Pascal Gessner:

Hi,

I'm trying to send an e-mail via confluence script.  
I'm using the following script:

```
import com.atlassian.confluence.mail.ConfluenceMailServerManager
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.mail.Email
import com.atlassian.sal.api.component.ComponentLocator

ConfluenceMailServerManager confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer()

if (mailServer) {
Email email = new Email("<my e-mail-address>")
email.setSubject("test")
email.setBody("test")
mailServer.send(email)
return 0
} else {
return 1
}
```

Unfortunately, when I try to execute it in the script console, I get this error:

```
java.util.ServiceConfigurationError: javax.mail.Provider: Provider com.sun.mail.imap.IMAPProvider not found
```

![2019-12-18 09_03_58-.png](https://community.atlassian.com/t5/image/serverpage/image-id/76620i2E733B30B856EB5C/image-size/large?v=v2&px=999 "2019-12-18 09_03_58-.png")

Sending a test-e-mail works as expected.  
Simply returning "mailServer" shows me the correct settings as well.

What am I doing wrong?

I'm using Confluence Server 7.0.2 and Scriptrunner 5.6.7.

Thanks for your help.


Answers:
author: Shannon S
body: Hello Pascal,

Thank you for contacting us about this.

Did this work in previous versions, or is it the first time you're trying this?

I would also recommend reaching out to Adaptavist directly, as it may be something that they're aware of.

You could also check your Confluence Server logs and see if there are any further details on what might cause this error.

Regards,

Shannon

comments: author: viadee Unternehmensberatung AG
body: Hello Shannon,

we get the same error while trying to send an e-mail even though we use a different method to send it without using Scriptrunner.  
Our code worked until we updated Confluence to Confluence 7.1.1 (from 6.15.9).

A more detailed discription of our problem can be found here: <https://community.developer.atlassian.com/t/sending-e-mails-with-confluence-7/33061>

Best regards,

Markus


author: Shannon S
body: Hi Markus,

Thank you for confirming the same. I'm not too familiar with scripting for Confluence, however, it may be that the script is not importing the correct JAVA package, because the stack trace isn't able to find that certain class (com.sun.mail.imap.IMAPProvider).

The folk who are best able to help you with that are indeed active on the Developer Community, so please let us know if you end up finding out through your thread there, and share the solution here.

Regards,

Shannon


author: Kurt Rosivatz
body: Hi,

we encounter the same Problems, running Confluence 7.0.5 and ScriptRunner 5.6.14.1-p5. We are running Confluence on Adopt OpenJDK.

Could it be that the com.sun.mail.imap.IMAPProvider is not available on OpenJDK and that that is the cause?

As Confluence itself can send e-mail in our configuration, it must use a mechanism that circumvents that problem. Would be nice if someone from Atlassian Team could give us a hint, how they send email ;-)

Regards,

Kurt


author: Shannon S
body: Thanks, everyone. I'm in touch with the developers here to see if we can determine if this is a bug or a change in Confluence.

One thing that the dev noted:

On the exception, it's complaining about the missing class:

```
com.sun.mail.imap.IMAPProvider
```

Having a look at Confluence, it contains the jar which does provide this class. So his recommendation is to try to do OSGI import **javax.mail\*;** from the plugin, which needs to use the above class, which could solve the problem.

Take care,

Shannon


author: Jan Kewitz
body: Hello,

thanks for the response. I finally got it to work using the @Import Annotation:

```
@Import({com.sun.mail.imap.IMAPProvider.class, com.sun.mail.smtp.SMTPProvider.class})
```

Regards,

Jan


author: Shannon S
body: Hi [@Jan Kewitz](/t5/user/viewprofilepage/user-id/3422540),

Thank you for letting me know how you were able to solve it!

[@viadee Unternehmensberatung AG](/t5/user/viewprofilepage/user-id/609844) [@Kurt Rosivatz](/t5/user/viewprofilepage/user-id/3697968) [@Pascal Gessner](/t5/user/viewprofilepage/user-id/1773178) -

Could you let me know if that helps in your case as well?

Shannon


author: Kurt Rosivatz
body: Hi [@Shannon S](/t5/user/viewprofilepage/user-id/510046)

it still does not work for me. As I'm working with Scriptrunner and my script is written in Groovy not Java, I can not use a solution that uses an import directive in Java code as Jan Kewitz did. Inspired by his code a added import statements and even tried loading the class via the class loader explicitly. Loading the class works, sending the mail still doesn't.

That's my code (I replaced actual e-mail address with receiver@example.com):

```
import com.atlassian.mail.Email;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.confluence.mail.ConfluenceMailServerManager
import com.atlassian.sal.api.component.ComponentLocator

// added code to test loading of com.sun.mail classes
import javax.mail.*
import com.sun.mail.imap.IMAPProvider
import com.sun.mail.smtp.SMTPProvider
new IMAPProvider()
new SMTPProvider()
// END added code to test loading of com.sun.mail classes

def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer();
Email email = new Email("receiver@example.com");
email.setSubject("test");
email.setBody("test");

// try loading that classes with the class loader of mailServer
mailServer.getClass().getClassLoader().loadClass("com.sun.mail.imap.IMAPProvider")
mailServer.getClass().getClassLoader().loadClass("com.sun.mail.smtp.SMTPProvider")
// try loading that classes with the class loader of mailServer

mailServer.send(email); // Exception occurs
```

Any help appreciated...

Regards,

Kurt


author: Kurt Rosivatz
body: Hi,

I solved it! After digging into Confluence source code, escpeciall JmxSmtpMailProvider, I tried getting the javax.mail.Session and added the IMAP and SMTP provider instances manually that is:

import com.sun.mail.imap.IMAPProvider  
import com.sun.mail.smtp.SMTPProvider

def imapProvider = new IMAPProvider()  
def smtpProvider = new SMTPProvider()

...

mailServer.getSession().addProvider(imapProvider)  
mailServer.getSession().addProvider(smtpProvider)

...

mailServer.send(email)

and **that works**! I could send without exception und received the email (at least in the script console).

Thats my complete script:

```
import com.atlassian.mail.Email;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.confluence.mail.ConfluenceMailServerManager
import com.atlassian.sal.api.component.ComponentLocator

import com.sun.mail.imap.IMAPProvider
import com.sun.mail.smtp.SMTPProvider

def imapProvider = new IMAPProvider()
def smtpProvider = new SMTPProvider()

def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer();
Email email = new Email("receiver@example.com");
email.setSubject("test");
email.setBody("test");

mailServer.getSession().addProvider(imapProvider)
mailServer.getSession().addProvider(smtpProvider)
mailServer.send(email)

```

HTH

Regards,

Kurt


author: Shannon S
body: Wow, thank you so much for sharing the fix! Hoping it can help others.

[@Pascal Gessner](/t5/user/viewprofilepage/user-id/1773178) [@viadee Unternehmensberatung AG](/t5/user/viewprofilepage/user-id/609844) Have a look at Kurt's resolution, and let us know if that helps you too!

Shannon


author: Pascal Gessner
body: Yep, works, at least in the script console - so I assume it would work in other places as well.

Thanks for the help!


author: Kurt Rosivatz
body: After looking at [an example from ScriptRunner documentation](https://scriptrunner.adaptavist.com/latest/confluence/recipes/connecting-to-conf-databases.html), I tried another solution. That works too and it looks a little bit cleaner to me. It also confirms that we had a class loading issue:

```
import com.atlassian.mail.Email;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.confluence.mail.ConfluenceMailServerManager
import com.atlassian.sal.api.component.ComponentLocator

def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer();
Email email = new Email("receiver@example.com");
email.setSubject("test");
email.setBody("test");

ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader()
Thread.currentThread().setContextClassLoader(SMTPMailServer.class.classLoader)
mailServer.send(email);
Thread.currentThread().setContextClassLoader(threadClassLoader)
```

This sends the email without errors.


author: Shannon S
body: [@Pascal Gessner](/t5/user/viewprofilepage/user-id/1773178), thank you for confirming that it is working for you, and thanks to [@Kurt Rosivatz](/t5/user/viewprofilepage/user-id/3697968) for providing an alternative solution for ScriptRunner!

Hopefully this can help anyone who runs into this issue int he future.

Take care, and have a pleasant rest of your week.

Shannon


author: Quinton Lephoto
body: [@Kurt Rosivatz](/t5/user/viewprofilepage/user-id/3697968) Thanks for the solutinon, I actually needed it for Jira software and it worked like magic but I used MailServerManager instead of ConfluenceMailServerManager


Tags: adaptavist

atl-adm

atlassian-marketplace

confluence-server