The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

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

JIRA ScriptRunner mail handler error

Lev Ryvkin
July 17, 2020

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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

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

DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events