Showing posts with label Message ID. Show all posts
Showing posts with label Message ID. Show all posts

Monday, November 7, 2016

Grails send email using grails.plugin.mail.MailService and track unique message ID

At first have to extend "grails.plugin.mail.MailService" as follows:

class MyMailService extends MailService

MyMailService would be like:

package com.pkm.services.mail

import grails.plugin.mail.MailMessageBuilder
import grails.plugin.mail.MailService
import org.springframework.mail.MailMessage
import org.springframework.mail.javamail.MimeMessageHelper

class MyMailService extends MailService {
    MailMessage sendMail(Closure callable) {
        if (isDisabled()) {
            throw new Exception("Sending emails disabled by configuration option")
            return null
        }

        MailMessageBuilder messageBuilder = mailMessageBuilderFactory.createBuilder(mailConfig)
        messageBuilder.multipart(true)

        /* Sending some custom headers with this email */
        Map<String, String> v = new HashMap<String,String>()
        v.put("header_1", "Header_1")
        v.put("header_2", "Header_2")
        messageBuilder.headers(v)

        callable.delegate = messageBuilder
        callable.resolveStrategy = Closure.DELEGATE_FIRST
        callable.call(messageBuilder)

        MailMessage mailMessage = messageBuilder.sendMessage(mailExecutorService)

        /* Reading unique message ID */
        MimeMessageHelper mimeMessageHelper = messageBuilder.getProperty("helper")
        String messageID = mimeMessageHelper.mimeMessage.getHeader("Message-ID").toString().trim()
        /* Now you can do your stuff with messageID */

        return mailMessage
    }
}
Use below code snippet to send mail
myMailService.sendMail() {
    mailSender.host = "host.mail.com"
    mailSender.javaMailProperties.put("mail.smtp.auth", "true")
    mailSender.username = "pritomkucse@gmail.com"
    mailSender.password = "xxxxxxxxx"
    mailSender.port = 569

    multipart true

    from "Pritom<pritomkucse@gmail.com>"
    to
    replyTo
    cc
    bcc
    subject
    text
    html
    attachBytes "some.name" "file_type" fileBytes
}
You need to add following dependency to BuildConfig.groovy as follows:
grails.project.dependency.resolution = {
    // inherit Grails' default dependencies
    inherits("global") {
        // specify dependency exclusions here; for example, uncomment this to disable ehcache:
        // excludes 'ehcache'
    }
    log "error" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
    checksums true // Whether to verify checksums on resolve
    legacyResolve false // whether to do a secondary resolve on plugin installation, not advised and here for backwards compatibility

    repositories {
        inherits true // Whether to inherit repository definitions from plugins

        grailsPlugins()
        grailsHome()
        mavenLocal()
        grailsCentral()
        mavenCentral()
        // uncomment these (or add new ones) to enable remote dependency resolution from public Maven repositories
        //mavenRepo "http://repository.codehaus.org"
        //mavenRepo "http://download.java.net/maven/2/"
        //mavenRepo "http://repository.jboss.com/maven2/"
    }

    dependencies {
        // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
        // runtime 'mysql:mysql-connector-java:5.1.29'
        // runtime 'org.postgresql:postgresql:9.3-1101-jdbc41'
        test "org.grails:grails-datastore-test-support:1.0.2-grails-2.4"
        compile "org.springframework:spring-orm:$springVersion"
        runtime 'mysql:mysql-connector-java:5.1.29'
    }

    plugins {
        // plugins for the build system only
        build ":tomcat:7.0.55"

        // plugins for the compile step
        compile ":scaffolding:2.1.2"
        compile ':cache:1.1.8'
        compile ":asset-pipeline:1.9.9"
        compile ":mail:1.0.5"

        // plugins needed at runtime but not for compilation
        runtime ":hibernate4:4.3.6.1" // or ":hibernate:3.6.10.18"
        runtime ":database-migration:1.4.0"
        runtime ":jquery:1.11.1"

        // Uncomment these to enable additional asset-pipeline capabilities
        //compile ":sass-asset-pipeline:1.9.0"
        //compile ":less-asset-pipeline:1.10.0"
        //compile ":coffee-asset-pipeline:1.8.0"
        //compile ":handlebars-asset-pipeline:1.3.0.3"
    }
}