Hi Guys,
Here is the code to send email in SoapUI using groovy.
Requirement: Java Mail Jar file location
http://www.java2s.com/Code/JarDownload/java/java-mail-1.4.4.jar.zip
// Note: Generally in SoapUI no need of java mail jar file but if require then can download from above link and save at ext directory.
Pre-Requisite Setting on Gmail for avoiding authentication error:
Before using below code follow below steps for Gmail access:
1. Login to Gmail with UserName and Password which you are going to use as the sender.
2. Then, open https://www.google.com/settings/security/lesssecureapps
3. Change the Security to “Turn On”.
After that update, the username and password in function calling and run the step.
Sending email "Gmail" SMPT server:
import com.sun.mail.smtp.SMTPTransport;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public static void Send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message) throws AddressException, MessagingException {
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.smtps.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtps.auth", "true");
/*
If set to false, the QUIT command is sent and the connection is immediately closed. If set
to true (the default), causes the transport to wait for the response to the QUIT command.
ref : http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html
http://forum.java.sun.com/thread.jspa?threadID=5205249
smtpsend.java - demo program from javamail
*/
props.put("mail.smtps.quitwait", "false");
Session session = Session.getInstance(props, null);
// -- Create a new message --
final MimeMessage msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(username + "@gmail.com"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));
if (ccEmail.length() > 0) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
}
msg.setSubject(title);
msg.setText(message, "utf-8");
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport)session.getTransport("smtps");
t.connect("smtp.gmail.com", username, password);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
}
Use below pattern to call the above create function:
log.info Send(username, password, recipientEmail, ccEmail, title, message);
Example:
log.info send("sampleUser", "SamplePassword", "samplerecipient@gmail.com", "sampleccmail@yahoo.com", "Test Subject", "Body of the mail")
Point to remember: While Calling does not give username complete email address, provide the only username like if Gmail-Id: samplegmailid@gmail.com then in function enter samplegmailid.
Original Post link: https://saurabhguptaqe.blogspot.in/2013/08/send-email-in-soap-ui-using-groovy.html
Cheers! Happy Testing!