Error while sending mail via script: IMAPProvider not found

Pascal Gessner December 18, 2019

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

1 vote
Answer accepted
Shannon S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 31, 2019

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

viadee Unternehmensberatung AG January 6, 2020

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

Shannon S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 10, 2020

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

Kurt Rosivatz February 3, 2020

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

Shannon S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 7, 2020

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

Jan Kewitz February 10, 2020

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

Like # people like this
Shannon S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 11, 2020

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 

Kurt Rosivatz February 18, 2020

Hi @Shannon S

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
Kurt Rosivatz February 19, 2020

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
Shannon S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 19, 2020

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

Pascal Gessner February 21, 2020

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
Kurt Rosivatz February 21, 2020

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. 

Like # people like this
Shannon S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 25, 2020

@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

Like # people like this
Quinton Lephoto September 21, 2020

@Kurt Rosivatz  Thanks for the solutinon, I actually needed it for Jira software and it worked like magic but I used MailServerManager instead of ConfluenceMailServerManager

Like Sam likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events