Send email with ScriptRunner

Hello

We would like to send a reminder to only the reporters of issues with status "Under Review" which haven't been updated for a week.

I then used the built-in script escalation service and tried to insert the following example I've found here on Answers, also applied the edits from the comments of said topic (topic) and other stuff.

import com.atlassian.mail.Email;
import com.atlassian.mail.server.MailServerManager;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.jira.ComponentManager;

subject = "test"
body = "test"
emailAddr = issue.getReporter().getEmailAddress()
 
def sendEmail(emailAddr, subject, body) {
  SMTPMailServer mailServer = ComponentManager.getInstance().getMailServerManager().getDefaultSMTPMailServer();
  if (mailServer) {
    Email email = new Email(emailAddr);
    email.setSubject(subject);
    email.setBody(body);
    mailServer.send(email);
  } else {
    // Problem getting the mail server from JIRA configuration, log this error
  }
}

sendEmail (emailAddr, subject, body)

However, ScriptRunner indicates that getMailServerManager can't be found and multiple other errors. When I checked the API of our current JIRA version, the desired class is actually not available anymore it seems or I just can't find it.

screenshot1.png

I then created my own version like so.

import com.atlassian.mail.Email;
import com.atlassian.mail.server.MailServerManager;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.jira.functest.framework.admin;

def subject = "test"
def body = "test"
def emailAddr = issue.getReporter().getEmailAddress()
 
def sendEmail(emailAddr, subject, body)
{
    SMTPMailServer mailServer = admin.MailServerAdministration.MailServerConfiguration.get()
    
    if (mailServer) 
    {
        Email email = new Email(emailAddr);
        email.setSubject(subject);
        email.setBody(body);
        mailServer.send(email);
	}
    
    else
    {
        // Problem getting the mail server from JIRA configuration, log this error
    }
}
sendEmail (emailAddr, subject, body)

But this indicates that the class com.atlassian.jira.functest.framework.admin can't be resolved which I've found in the API documentation for JIRA 7.1.4

screenshot2.png

Can somebody help me here?

Thanks

1 answer

1 accepted

Hi there,

You said you are in a JIRA v7.* , so the ComponentManager is deprecated and you should use the ComponentAccessor instead. therefore in the first script you posted you will be able to get the mailServerManager (and then the SMTPServer) like 

import com.atlassian.jira.component.ComponentAccessor


def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()

regards

thanos

Hi Thanos

Thanks. This works so far.

But now it says it can't find a matching method for

new Email(emailAddr);

I just checked the API again and this class and method are available in 7.1.4 and the syntax seems correct.

Any ideas?

Thanks for your help.

The 'errors' you see in the script runner are because of the Static Type Checking, in a dynamically-typed language. Therefore some times is safe to ignore them. So if you declare the type of your params 

def sendEmail(String emailAddr, String subject, String body) { ... }

you will see the difference.

regards

Thanos

Ah, I see. It works now! Thanks a lot for your help and the helpful link.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Apr 30, 2018 in Marketplace Apps

Why moving to Confluence Data Center is only a job half-done

Confluence Data Center is a deployment option that was designed for performance at scale. In other words, Confluence will handle increased traffic much reliably in a clustered environment and yo...

225 views 0 6
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you