cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
260
Views
0
Helpful
0
Comments
cdnadmin
Community Member
This document was generated from CDN thread

Created by: Dipika Gupta on 26-07-2013 10:45:04 AM
Hi ,

I am new to Customization of elements using java code and not at all great at java
I have a custom element which is sending email and capturing any error but just adding it to the log .
I want to capture this error as element data so that I can playback an error message to caller and ask him to try again.
Can you please guide me where and what to put the code for setting the error captured as element data.

Below is my java code


import com.audium.server.AudiumException;
import com.audium.server.session.ActionElementData;
import com.audium.server.voiceElement.*;
import com.audium.server.xml.ActionElementConfig;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;

public class SendMail extends ActionElementBase
    implements ElementInterface
{

    public SendMail()
    {
    }

    public String getElementName()
    {
        return "SendMail";
    }

    public String getDisplayFolderName()
    {
        return "Custom Elements";
    }

    public String getDescription()
    {
        return "This action element Sends EMail";
    }

    public Setting[] getSettings()
        throws ElementException
    {
        Setting settingArray[] = new Setting[5];
        settingArray[0] = new Setting("To", "To", "[email protected]", true, true, true, 3);
        settingArray[1] = new Setting("Subject", "Subject", "Subject", true, true, true, 3);
        settingArray[2] = new Setting("Body", "Body", "Body", true, true, true, 3);
        settingArray[3] = new Setting("Attachment", "Attachment", "Attachment", false, true, true, 3);
        settingArray[4] = new Setting("Filename", "Filename", "Filename", false, true, true, 3);
        return settingArray;
    }

    public ExitState[] getExitStates()
        throws ElementException
    {
        ExitState exitStateArray[] = new ExitState[1];
        exitStateArray[0] = new ExitState("done", "done", "done");
        return exitStateArray;
    }

    public void doAction(String name, ActionElementData actionData)
        throws AudiumException
    {
        ActionElementConfig config = actionData.getActionElementConfig();
        String To = config.getSettingValue("To", actionData).trim();
        String Subject = config.getSettingValue("Subject", actionData).trim();
        String Body = config.getSettingValue("Body", actionData).trim();
        String Attachment = config.getSettingValue("Attachment", actionData);
        String Filename = config.getSettingValue("Filename", actionData);
        if(Attachment == null)
        {
            actionData.addToLog("Mail", "No Atttachment");
            send("[email protected] ", To, Subject, Body, actionData);
        } else
        {
            actionData.addToLog("Mail", "With Atttachment");
            sendAttachment("[email protected] ", To, Subject, Body, Attachment, Filename, actionData);
        }
    }

    private void send(String from, String to, String subject, String text, ActionElementData actionData)
    {
        Properties props = new Properties();
        props.put("mail.smtp.host", "ustu-zone.relay.testing.com");
        props.put("mail.smtp.port", "25");
        Session mailSession = Session.getDefaultInstance(props);
        Message simpleMessage = new MimeMessage(mailSession);
        InternetAddress fromAddress = null;
        InternetAddress toAddress[] = (InternetAddress[])null;
        try
        {
            fromAddress = new InternetAddress(from);
            to = to.replace(";", ",");
            toAddress = InternetAddress.parse(to);
        }
        catch(AddressException e)
        {
            e.printStackTrace();
        }
        try
        {
            simpleMessage.setFrom(fromAddress);
            simpleMessage.setRecipients(javax.mail.Message.RecipientType.TO, toAddress);
            simpleMessage.setSubject(subject);
            simpleMessage.setText(text);
            actionData.addToLog("Email Sent", to);
            Transport.send(simpleMessage);
        }
        catch(MessagingException e)
        {
            actionData.addToLog("Failed to Send Email", "");
            actionData.addToLog("Error", e.toString());
        }
    }

    private void sendAttachment(String from, String to, String subject, String text, String Attachment, String Filename, ActionElementData actionData)
    {
        Properties props = new Properties();
        props.put("mail.smtp.host", "ustu-zone.relay.testing.com");
        props.put("mail.smtp.port", "25");
        String fileAttachment = Attachment;
        Session session = Session.getInstance(props, null);
        try
        {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(text);
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            messageBodyPart = new MimeBodyPart();
            javax.activation.DataSource source = new FileDataSource(fileAttachment);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(Filename);
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
            Transport.send(message);
            actionData.addToLog("Email sent to", to);
        }
        catch(Exception e)
        {
            actionData.addToLog("Failed to Send Email", "");
            actionData.addToLog("Error", e.toString());
        }
    }
}

Subject: RE: How to set element data in custom java code
Replied by: Hemal Mehta on 26-07-2013 11:17:23 AM
There are different ways to do it.  In your catch error block you can set something like:
catch(Exception e)
{
actionData.addToLog("Failed to Send Email", "");
actionData.addToLog("Error", e.toString());
//Define this string before
returnStatus=”error”;
}
 actionData.setElementData("returnStatus",returnStatus);
 
Check this returnStatus after the action element and play to the caller what you want.
Other  way you can do is to implement this as a decision element and define the exit state for error, success etc and branch out and play the appropriate message for error to the caller.
Hemal
Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Quick Links