Sending Emails from a WSO2 Carbon Component

First we need to enable the mail transporter in the axis2.xml file. For that, add (or uncomment) the mail transport sender as follows.
    <transportSender name="mailto" class="org.apache.axis2.transport.mail.MailTransportSender">
        <parameter name="mail.smtp.from">username@gmail.com</parameter>
        <parameter name="mail.smtp.user">username</parameter>
        <parameter name="mail.smtp.password">password</parameter>
        <parameter name="mail.smtp.host">smtp.gmail.com</parameter>
        <parameter name="mail.smtp.port">587</parameter>
        <parameter name="mail.smtp.starttls.enable">true</parameter>
        <parameter name="mail.smtp.auth">true</parameter>
    </transportSender>

Here set your gmail account's user credentials for 'username' and 'password'.

Then we need to load the axis2 configurations to the our carbon component. This should be done when activating the bundle (i.e. in the bundle activator) in which we are going to send the email. For that, add the @scr.reference name="configurationcontext.service" interface="org.wso2.carbon.utils.ConfigurationContextService" cardinality="1..1" policy="dynamic" bind="setConfigurationContextService" unbind="unsetConfigurationContextService" annonation to the bundle activator class, and add the following two methods to the same class.
protected void setConfigurationContextService(ConfigurationContextService configurationContextService){
MLEmailAdaptorValueHolder.registerConfigurationContextService(configurationContextService);
} protected void unsetConfigurationContextService(ConfigurationContextService configurationContextService){
MLEmailAdaptorValueHolder.registerConfigurationContextService(null);
}
Here MLEmailAdaptorValueHolder is a simple java bean just to hold the Axis2 ConfigurationContext object. It will basically look like follows.
public class MLEmailAdaptorValueHolder {
private static ConfigurationContextService configurationContextService; public static void registerEmailAdaptorService(EmailAdaptorService emailAdaptorService){
MLEmailAdaptorValueHolder.emailAdaptorService = emailAdaptorService;
} public static EmailAdaptorService getEmailAdaptorService(){
return emailAdaptorService;
} public static void registerConfigurationContextService(ConfigurationContextService configurationContextService) {
MLEmailAdaptorValueHolder.configurationContextService = configurationContextService;
}
}
Now, the following function can be used to create and send the email.
private void send(String receiverEmailAddress, String emailSubject, String emailBody,) throws Exception {
try {
// Set the subject of the email. Map<String, String> headerMap = new HashMap<String, String>(); headerMap.put(MailConstants.MAIL_HEADER_SUBJECT, emailSubject); OMElement payload = OMAbstractFactory.getOMFactory().createOMElement(BaseConstants.DEFAULT_TEXT_WRAPPER, null);  payload.setText(emailBody); // Create a service client using configurations in axis2.xml ServiceClient serviceClient; ConfigurationContext configContext = MLEmailAdaptorValueHolder.getConfigurationContextService().getServerConfigContext(); if (configContext != null) {     serviceClient = new ServiceClient(configContext, null); } else {     serviceClient = new ServiceClient(); } // Set additional properties of the service client Options options = new Options(); options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE); options.setProperty(MessageContext.TRANSPORT_HEADERS, headerMap); options.setProperty(MailConstants.TRANSPORT_MAIL_FORMAT, MailConstants.TRANSPORT_FORMAT_TEXT); options.setTo(new EndpointReference("mailto:" + receiverEmailAddress)); serviceClient.setOptions(options); // Send the mail with the payload serviceClient.fireAndForget(payload); logger.info("Sending confirmation mail to " + receiverEmailAddress);
} catch (AxisFault e) {
throw new EmailAdaptorException("An error occured while sending the email: " + e.getMessage(), e);
}
}

Share:

1 comments