Send email via task and groovy script

groovy

#1

Hi all !

I created a groovy script which remove oldest components of a maven release repo. It is schedule by a task in Nexus administration.

I want to improve this script by sending notification of incoming deletion (some days before).

My issue : how could i send email from this script ?
I tried:

import javax.mail.*
import javax.mail.internet.*

....

    private boolean sendMail(){
            def d_email = "email@email.com",
            d_host = "smtpo365",
            d_port  = "25",
            m_to = "email@email.com",
            m_subject = "Testing",
            m_text = "Hey, this is the testing email."

            def props = new Properties()
            props.put("mail.smtp.user", d_email)
            props.put("mail.smtp.host", d_host)
            props.put("mail.smtp.port", d_port)
            props.put("mail.smtp.starttls.enable","false")
            props.put("mail.smtp.debug", "true");
            props.put("mail.smtp.auth", "false")
            props.put("mail.smtp.socketFactory.port", d_port)
            //props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory")
            props.put("mail.smtp.socketFactory.fallback", "false")

            def auth = new SMTPAuthenticator()
            def session = Session.getInstance(props, auth)
            session.setDebug(true);

            def msg = new MimeMessage(session)
            msg.setText(m_text)
            msg.setSubject(m_subject)
            msg.setFrom(new InternetAddress(d_email))
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to))

            Transport transport = session.getTransport("smtps");
            transport.connect(d_host, d_port);
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
        }

....

Result :
unable to resolve class SMTPAuthenticator
def auth = new SMTPAuthenticator()

Do somebody have an idea of my mistake or a way to do this email sending ?

Thanks alot !


#2

Something working…

import org.sonatype.nexus.email.EmailConfiguration
import org.sonatype.nexus.email.EmailManager
import org.apache.commons.mail.SimpleEmail

    def emailMgr = container.lookup(EmailManager.class.getName());
    SimpleEmail email = new SimpleEmail();
    email.setSubject("Nexus release clean-up task");
    email.setMsg("This is a test mail ... :-)");
    email.addTo("my@email.com");
    emailMgr.send(email)

#3

I’m trying to go further : using org.apache.commons.mail.HtmlEmail

Then this error is throwed :
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/alternative

Maybe something like that could work : groovy - Sending an email: no object DCH for MIME type multipart/mixed - Stack Overflow ?