Send email with ScriptRunner

PFS August 10, 2016

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

6 votes
Answer accepted
Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 10, 2016

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

PFS August 10, 2016

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.

Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 10, 2016

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

PFS August 11, 2016

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

Dan27 October 9, 2018

Hi @Thanos Batagiannis _Adaptavist_ ,

I try using this code, without success:

Got errors:

2018-10-09 08:22:53,337 WARN [runner.ScriptRunnerImpl]: Please make sure that a valid mailServer is configured
2018-10-09 08:22:53,337 WARN [runner.ScriptRunnerImpl]: java.lang.NullPointerException: Cannot invoke method send() on null object

My code:

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import com.atlassian.jira.issue.issuetype.IssueType
import javax.ws.rs.core.Response
import javax.ws.rs.core.MultivaluedMap
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.config.IssueTypeManager;
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletRequest;

try {

sendEmail('daniel_mor@bmc.com','Mail','Hi');


}
catch (e) {
}

 

// Create an email
def sendEmail(String emailAddr, String subject, String body) {
//MailServerManager mailServerManager = ComponentAccessor.getMailServerManager();
def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer();

//if (mailServer) {
try{
Email email = new Email(emailAddr)
email.setSubject(subject)
email.setBody(body)
mailServer.send(email)
log.debug("Mail sent")
} catch(Exception e) {
log.warn("Please make sure that a valid mailServer is configured")
log.warn e;
}
}

Like # people like this
Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 9, 2018

Hi Daniel,

Well the logs give you a hint

Please make sure that a valid mailServer is configured

Configuring an SMTP mail server

Regards, Thanos

Like # people like this
Dan27 October 21, 2019

@Thanos Batagiannis _Adaptavist_  Thank you Thanos!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events