It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Error while sending mail via script: IMAPProvider not found Edited

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

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.

1 answer

1 accepted

0 votes
Answer accepted

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

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

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

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

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

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

Hi @Jan Kewitz,

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

@viadee_Unternehmensberatung_AG @Kurt_Rosivatz @Pascal_Gessner -

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

Shannon 

Hi @Shannon_Spaniol

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

Like Pascal_Gessner likes this

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

Like # people like this

Wow, thank you so much for sharing the fix! Hoping it can help others.

@Pascal_Gessner @viadee_Unternehmensberatung_AG Have a look at Kurt's resolution, and let us know if that helps you too!

Shannon

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

Thanks for the help!

Like Kurt_Rosivatz likes this

After looking at an example from ScriptRunner documentation, 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. 

@Pascal_Gessner, thank you for confirming that it is working for you, and thanks to @Kurt_Rosivatz 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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Confluence

How is your team having fun and bonding, remotely, utilizing Confluence?

Thanks everyone for answering last week’s question. The winner of the random drawing from those who commented is: @LarryBrock I’ll contact you separately with your prize details. This wee...

283 views 9 7
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you