Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

JIRA ScriptRunner mail handler error

Lev Ryvkin July 17, 2020 edited

Hello!

I'm trying to create custom ScriptRunner Mail Handler to create issue with attachments from incoming mail.

Here is the code

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.util.JiraHome
import com.atlassian.jira.service.services.file.FileService
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.mail.MailUtils
import org.apache.commons.io.FileUtils

def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)

def subject = message.getSubject() as String
def issue = ServiceUtils.findIssueObjectInString(subject)
if (issue) {
return
}

ApplicationUser user = userManager.getUserByName("admin")
ApplicationUser defaultReporter = userManager.getUserByName("admin")
def project = projectManager.getProjectObjByKey("HL")

def issueObject = issueFactory.getIssue()
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Bug" }.id)
issueObject.setReporter(defaultReporter)
issue = messageHandlerContext.createIssue(user, issueObject)

def attachments = MailUtils.getAttachments(message)

attachments.each { MailUtils.Attachment attachment ->
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachment.filename) as File
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issue)
}

The problem is: issue is created successfully, but there is no attachment added and message file was not deleted after processing. Run Now test button returns a strange error:

Other:

C:\Program Files\Atlassian\Application Data\JIRA\import\mail\__utf_8_B_Rlc6IEVYVF0g0J3QvtCy0YvQuSDQvtGC0LLQtdGCINC90LAg0LDQvdC60LU___ ___utf_8_B_0YLRgyAi0JPQvtGA0Y_Rh9Cw0Y8g0LvQuNC90LjRjyI___.eml (The filename, directory name, or volume label syntax is incorrect)

I am exporting .eml from our exchange mailbox to \import\mail\ directory. Every message has a default name template as "message_0.eml", "message_1.eml" etc. 

1 answer

1 accepted

1 vote
Answer accepted
Lev Ryvkin July 17, 2020

I've found out myself. The problem was in this block:

def attachments = MailUtils.getAttachments(message)

attachments.each { MailUtils.Attachment attachment ->
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachment.filename) as File
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issue)
}

 My eml file was containing .msg file as attachment. This attachment was exported from eml file and was forcefully converted to eml from msg. It also had "[] :" symbols in its name which were lost during base64 encoding-decoding process.

The problem was resolved by using static name string "Attachment.eml" instead of attachment.filename variable

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
atlassian, confluence, loom, atlassian intelligence, ai notes, ai-powered meeting notes, atlassian community events, ace, confluence ai, loom ai integration, ai note-taking, atlassian ai features, team '25, atlassian live learning, confluence automation

Unlock AI-powered meeting notes: Join our live learning session! 📹

Did you catch the news at Team ‘25? With Loom, Confluence, Atlassian Intelligence, & even Jira 👀, you won’t have to worry about taking meeting notes again… unless you want to. Join us to explore the beta & discover a new way to boost meeting productivity.

Register today!
AUG Leaders

Atlassian Community Events