SOAP Mesajı üzerinden Mail Atma
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/Yardimci/EPosta/v1">
<soapenv:Header/>
<soapenv:Body>
<v1:EPostaGonder>
<!--Optional:-->
<v1:ePostaBilgisi>
<!--Optional:-->
<v1:Alicilar>
<!--Zero or more repetitions:-->
<v1:mailAdresi>info@apinizer.com</v1:mailAdresi>
</v1:Alicilar>
<!--Optional:-->
<v1:BilgiAlicilar>
<!--Zero or more repetitions:-->
<v1:mailAdresi></v1:mailAdresi>
</v1:BilgiAlicilar>
<!--Optional:-->
<v1:GizliBilgiAlicilar>
<!--Zero or more repetitions:-->
<v1:mailAdresi></v1:mailAdresi>
</v1:GizliBilgiAlicilar>
<v1:Gonderen>support@apinizer.com</v1:Gonderen>
<v1:Icerik>Örnek içerik</v1:Icerik>
<v1:Konu>Örnek Konu adı</v1:Konu>
<v1:MesajTuru>text/html;charset=utf-8</v1:MesajTuru>
</v1:ePostaBilgisi>
</v1:EPostaGonder>
</soapenv:Body>
</soapenv:Envelope>
RESPONSE SAMPLE
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<out:EPostaGonderResponse xmlns:out="http://schema.apinizer.com/Yardimci/EPosta/v1">
<out:EPostaGonderResult>
<out:IslemBasariliMi>true</out:IslemBasariliMi>
</out:EPostaGonderResult>
</out:EPostaGonderResponse>
</soapenv:Body>
</soapenv:Envelope>
*/
def createSoapResponse( def IslemBasariliMi, def HataMesaji, def HataKodu){
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.EPostaGonderResponse' ( ){
'out.EPostaGonderResult' ( ){
'out.IslemBasariliMi'(IslemBasariliMi)
if(!IslemBasariliMi){
'out.HataMesaji'(HataMesaji)
'out.HataKodu'(HataKodu)
}
}
}
}
}
}
return XmlUtil.serialize(soapRequest)
}
try {
requestHeaderMapFromClient.put('statusCodeToTargetAPI',0)
def Envelope = new XmlSlurper().parseText(requestBodyTextFromClient)
def ePostaBilgisi= Envelope.Body.EPostaGonder.ePostaBilgisi
String to=""
int toSize=ePostaBilgisi.Alicilar.mailAdresi.size()
for(int i=0;i<toSize;i++){
to+=ePostaBilgisi.Alicilar.mailAdresi[i].text()+","
}
to= StringUtils.removeEnd(to, ",")
String cc=""
int ccSize=ePostaBilgisi.BilgiAlicilar.mailAdresi.size()
for(int i=0;i<ccSize;i++){
cc+=ePostaBilgisi.BilgiAlicilar.mailAdresi[i].text()+","
}
cc= StringUtils.removeEnd(to, ",")
String bcc=""
int bccSize=ePostaBilgisi.GizliBilgiAlicilar.mailAdresi.size()
for(int i=0;i<bccSize;i++){
bcc+=ePostaBilgisi.GizliBilgiAlicilar.mailAdresi[i].text()+","
}
bcc= StringUtils.removeEnd(to, ",")
String from=ePostaBilgisi.Gonderen.text()
if(!from?.trim()){
requestErrorMessageToTargetAPI = createSoapResponse(false,'En az bir kimden bilgisi eklenmeli!','01-ERR-03' )
statusCodeToTargetAPI=400
return
}
if(!to?.trim()&& !cc?.trim()&& !bcc?.trim()){
requestErrorMessageToTargetAPI = createSoapResponse(false,'En az bir alıcı eklenmeli!','01-ERR-04' )
statusCodeToTargetAPI=400
return
}
String subject=ePostaBilgisi.Konu
if(!subject?.trim()){
subject=""
}
String content=ePostaBilgisi.Icerik
if(!content?.trim()){
content=""
}
String mimeType=ePostaBilgisi.MesajTuru
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,'Mail gönderirken hata oluştu:' + e.getMessage(),'01-ERR-05' )
return
}
statusCodeToTargetAPI=200
requestErrorMessageToTargetAPI = createSoapResponse(true,'' ,'' )
} catch (Exception e) {
e.printStackTrace()
statusCodeToTargetAPI=500
requestErrorMessageToTargetAPI = createSoapResponse(false,'Mail gönderirken hata oluştu:' + e.getMessage(),'01-ERR-06' )
return
}
GROOVY