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:
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.
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
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!Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.