Unable to send email JavaMail API in MFP 7.0 Java adapter

182 views Asked by At

I am using below code send email via java adapter in MFP 7.0 it has attachment also.

try {
        StringBuffer sb = new StringBuffer();
        try {
            sb.append("<p style='font-family:Sans-serif;font-size: 12px'>Dear Xyz,<br><br>Attachment :</p>");
            sb.append("<p style='font-family:Sans-serif;font-size: 12px'>Regards, <br><br>  Team");
            String mailHost = "10.x.x.x";
            String mailFrom = "[email protected]";
            String mailTo = email;
            String mailSubject = "Subject";
            String mailBody = sb.toString();
            String mailAttachment = "" + fileName;

            Properties properties = System.getProperties();
            properties.setProperty("mail.smtp.host", mailHost);

            Session session = Session.getDefaultInstance(properties);

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(mailFrom));
            Address[] toAddress = null;

            if (mailTo != null) {
                toAddress = InternetAddress.parse(mailTo);
                message.setRecipients(Message.RecipientType.TO, toAddress);
            }

            message.setSubject(mailSubject);

            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(mailBody, "text/html");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            messageBodyPart = new MimeBodyPart();
            String attachmentLocation = mailAttachment;
            DataSource source = new FileDataSource(attachmentLocation);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(new File(attachmentLocation)
                    .getName());
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);

            Transport.send(message);
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }

I am getting the below error.

java.lang.ClassCastException: org.apache.geronimo.mail.handlers.HttpHandler cannot be cast to javax.activation.DataContentHandler jax rs

Kindly suggest on this issue as the class HttpHandler is from worklight-jee-library.jar which i am unable override. Is their any alternative for sending email with attachment from Java adapter.

1

There are 1 answers

0
Idan Adar On

We do it a bit differently...

  1. In the application logic:

    ...
    ...    
    for (var i = 0; i < devices.resultSet.length; i++) {
        var sendResult = com.SendMail.SendMail.sendMail(devices.resultSet[i].email, devices.resultSet[i].serial, devices.resultSet[i].manufacturer, devices.resultSet[i].days);
        results.push(sendResult);
    }
    ...
    
  2. And the Java code (stored in server/java/com/SendMail/SendMail.java) - you will obviously need to change the implementation details...:

    package com.SendMail;
    
    import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    
    public class SendMail {
        public static String sendMail(String emailAddress) {
         String debugMessage = "";
    
    
              // Recipient's email ID needs to be mentioned.
              //String to = "[email protected]";
              String to = emailAddress;
    
              // Sender's email ID needs to be mentioned
              String from = "the-senders-email-address";
    
              Properties props = new Properties();
              props.put("mail.smtp.host", "the.host.name.com");
              props.put("mail.smtp.port", "25");
    
              // Get the default Session object.
              Session session = Session.getDefaultInstance(props);
    
              try{
                 // Create a default MimeMessage object.
                 MimeMessage message = new MimeMessage(session);
    
                 // Set From: header field of the header.
                 message.setFrom(new InternetAddress(from));
    
                 // Set To: header field of the header.
                 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    
                 // Set Subject: header field
                 message.setSubject("the email subject line");
    
                 // Now set the actual message
                 message.setText("the email body message");
    
    
                 // Send message
                 Transport.send(message);
                 debugMessage="Email sent successfully to " + emailAddress;
    
              } catch (MessagingException mex) {
                 mex.printStackTrace();
                 debugMessage=mex.toString();
              }
    
              return debugMessage;
        }
    }