0
votes

I am trying to send an email that contains a Test Step result in SOAPUI using Groovy script. I first wrote the code on my Groovy Console and it worked fine. When I copied and pasted it in SOAPUI (Groovy script test Step) it showed an error. Here is my code:

I am trying to send an email that contains a Test Step result in SOAPUI using Groovy script. I first wrote the code on my Groovy Console and it worked fine. When I copied and pasted it in SOAPUI (Groovy script test Step) it showed an error. Here is my code

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

public class SMTPAuthenticator extends Authenticator
{
    public PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication('[email protected]', '****');
    }
}

def  d_email = "[email protected]",
        d_uname = "test",
        d_password = "*****",
        d_host = "smtp.gmail.com",
        d_port  = "465", //465,587
        m_to = "[email protected]",
        m_subject = "Testing",
        m_text = "Hi, 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","true")
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true")
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, 465, d_uname, d_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

The problem is with this line : transport.sendMessage(msg, msg.getAllRecipients());

When I delete this line, there is no errpr but the message is not sent..

Do you know why this works perfectly on groovy console but throws error in Soapui

Thank you for your help in advance!

1

1 Answers

0
votes

I tested your code and it works, except for 2 things:

1.  Run the script with 'groovy.bat -classpath mail-1.4.1.jar sendemail.groovy'
2.  Fix the 2 misspellings of gmail in the script.