Sending Mail over SOAP Message
import javax.mail.*
import org.apache.commons.lang3.StringUtils
import javax.mail.internet.*
import groovy.util.XmlSlurper
import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil
/*
REQUEST SAMPLE
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://schema.apinizer.com/Helper/EMail/v1">
<soapenv:Header/>
<soapenv:Body>
<v1:SendEMail>
<!--Optional:-->
<v1:eMailInfo>
<!--Optional:-->
<v1:Recipients>
<!--Zero or more repetitions:-->
<v1:mailAdrress>info@apinizer.com</v1:mailAdrress>
</v1:Recipients>
<!--Optional:-->
<v1:infoRecipients>
<!--Zero or more repetitions:-->
<v1:mailAdrress></v1:mailAdrress>
</v1:infoRecipients>
<!--Optional:-->
<v1:SecretRecipitnets>
<!--Zero or more repetitions:-->
<v1:mailAdrress></v1:mailAdrress>
</v1:SecretRecipitnets>
<v1:Sender>support@apinizer.com</v1:Sender>
<v1:Content>Sample content</v1:Content>
<v1:Subject>Sample subject</v1:Subject>
<v1:MessageType>text/html;charset=utf-8</v1:MessageType>
</v1:eMailInfo>
</v1:SendEMail>
</soapenv:Body>
</soapenv:Envelope>
RESPONSE SAMPLE
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<out:SendEMailResponse xmlns:out="http://schema.apinizer.com/Helper/EMail/v1">
<out:SendEMailResult>
<out:isTransactionSuccess>true</out:isTransactionSuccess>
</out:SendEMailResult>
</out:SendEMailResponse>
</soapenv:Body>
</soapenv:Envelope>
*/
def createSoapResponse( def isTransactionSuccess, def ErrorMessage, def ErrorCode){
def builder = new StreamingMarkupBuilder()
builder.encoding ='utf-8'
def soapRequest = builder.bind {
mkp.declareNamespace( 'soapenv': "http://schemas.xmlsoap.org/soap/envelope/" )
mkp.declareNamespace( 'out': "http://schema.apinizer.com/Yardimci/EPosta/v1")
mkp.declareNamespace( 'xsi': "http://www.w3.org/2001/XMLSchema-instance" )
'soapenv:Envelope' {
'soapenv:Header'()
'soapenv:Body' {
'out.SendEMailResponse' ( ){
'out.SendEMailResult' ( ){
'out.isTransactionSuccess'(isTransactionSuccess)
if(!isTransactionSuccess){
'out.ErrorMessage'(ErrorMessage)
'out.ErrorCode'(ErrorCode)
}
}
}
}
}
}
return XmlUtil.serialize(soapRequest)
}
try {
requestHeaderMapFromClient.put('statusCodeToTargetAPI',0);
def Envelope = new XmlSlurper().parseText(requestBodyTextFromClient);
def eMailInfo= Envelope.Body.SendEMail.eMailInfo;
String to="";
int toSize=eMailInfo.Recipients.mailAdrress.size();
for(int i=0;i<toSize;i++){
to+=eMailInfo.Recipients.mailAdrress[i].text()+",";
}
to= StringUtils.removeEnd(to, ",");
String cc="";
int ccSize=eMailInfo.infoRecipients.mailAdrress.size();
for(int i=0;i<ccSize;i++){
cc+=eMailInfo.infoRecipients.mailAdrress[i].text()+",";
}
cc= StringUtils.removeEnd(to, ",");
String bcc="";
int bccSize=eMailInfo.secretRecipients.mailAdrress.size();
for(int i=0;i<bccSize;i++){
bcc+=eMailInfo.secretRecipients.mailAdrress[i].text()+",";
}
bcc= StringUtils.removeEnd(to, ",");
String from=eMailInfo.Sender.text() ;
if(!from?.trim()){
requestErrorMessageToTargetAPI = createSoapResponse(false,'At least one from must be added','01-ERR-03' );
statusCodeToTargetAPI=400;
return;
}
if(!to?.trim()&& !cc?.trim()&& !bcc?.trim()){
requestErrorMessageToTargetAPI = createSoapResponse(false,'At least one recipient must be added','01-ERR-04' );
statusCodeToTargetAPI=400;
return;
}
String subject=eMailInfo.Subject;
if(!subject?.trim()){
subject=""
}
String content=eMailInfo.Content;
if(!content?.trim()){
content=""
}
String mimeType=eMailInfo.MessageType;
if(!mimeType?.trim()){
mimeType="text/html;charset=utf-8"
}
// Init constants of sender email account.
String email = "info@apinizer.com"
String password = "xxxx"
String host = "mail.apinizer.com"
String port = "587" // "465" "587"
// Set up properties.
Properties props = System.getProperties()
props.put("mail.smtp.user", email)
props.put("mail.smtp.host", host)
props.put("mail.smtp.port", port)
props.put("mail.smtp.auth", "true")
props.put("mail.smtp.starttls.enable","false")
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory")
props.put("mail.smtp.ssl.trust", host) // Change host to "*" if you want to trust all host.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(email, password);
}
});
// Set up message.
MimeMessage message = new MimeMessage(session)
message.setFrom(new InternetAddress(from))
if(to?.trim()){
String[] toStrArr=to.split(",");
for (String m:toStrArr){
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse( m))
}
}
if(cc?.trim()){
String[] ccStrArr=cc.split(",");
for (String m:ccStrArr){
message.addRecipients(Message.RecipientType.CC, InternetAddress.parse(m))
}
}
if(bcc?.trim()){
String[] bccStrArr=bcc.split(",");
for (String m:bccStrArr){
message.addRecipients(Message.RecipientType.BCC, InternetAddress.parse(m))
}
}
message.setSubject(subject)
message.setContent(content, mimeType)
try {
// Send mail.
Transport.send(message )
} catch (Exception e) {
e.printStackTrace();
statusCodeToTargetAPI=500;
requestErrorMessageToTargetAPI = createSoapResponse(false,'Error occured while sending mail:' + e.getMessage(),'01-ERR-05' );
return;
}
statusCodeToTargetAPI=200;
requestErrorMessageToTargetAPI = createSoapResponse(true,'' ,'' );
} catch (Exception e) {
e.printStackTrace();
statusCodeToTargetAPI=500;
requestErrorMessageToTargetAPI = createSoapResponse(false,'Error occured while sending mail:' + e.getMessage(),'01-ERR-06' );
return;
}
GROOVY