簡體   English   中英

無法在MFP 7.0 Java適配器中發送電子郵件JavaMail API

[英]Unable to send email JavaMail API in MFP 7.0 Java adapter

我正在使用以下代碼通過MFP 7.0中的Java適配器發送電子郵件,它也具有附件。

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();
        }

我收到以下錯誤。

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

請就這個問題提出建議,因為類HttpHandler來自worklight-jee-library.jar,我無法覆蓋它。 是從Java適配器發送帶有附件的電子郵件的替代方法嗎?

我們做的有點不同...

  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; } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM