无法在MFP 7.0 Java适配器中发送电子邮件JavaMail API

时间:2016-11-03 14:27:45

标签: java ibm-mobilefirst javamail geronimo

我正在使用下面的代码通过MFP 7.0中的java适配器发送电子邮件,它也有附件。

var myConfiguration = new MyConfiguration();
var watermarkName = myConfiguration.WatermarkConfiguration.FileName;

我收到以下错误。

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 = "xyz@xyz.com";
            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();
        }

请在这个问题上建议,因为HttpHandler类来自worklight-jee-library.jar,我无法覆盖。他们可以选择从Java适配器发送带附件的电子邮件。

1 个答案:

答案 0 :(得分:0)

我们做的有点不同......

  1. 在应用程序逻辑中:

    ...
    ...    
    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. Java代码(存储在server / java / com / SendMail / SendMail.java中) - 您显然需要更改实现细节......:

    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 = "the@email.address";
              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;
        }
    }