无法在 MFP 7.0 Java 适配器中发送电子邮件 JavaMail API
Posted
技术标签:
【中文标题】无法在 MFP 7.0 Java 适配器中发送电子邮件 JavaMail API【英文标题】:Unable to send email JavaMail API in MFP 7.0 Java adapter 【发布时间】:2016-11-03 14:27:44 【问题描述】:我在 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 适配器发送带有附件的电子邮件。
【问题讨论】:
看起来您使用的是 Gnu 版本的 JavaMail。如果您可以切换到JavaMail reference implementation,您可能会更幸运。如果 Gnu 版本嵌入在 worklight 库中,并且您无法更改它,您可能需要联系 IBM 寻求帮助。 @arun punnath,这个问题有什么更新吗? 【参考方案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);
...
还有 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;
【讨论】:
以上是关于无法在 MFP 7.0 Java 适配器中发送电子邮件 JavaMail API的主要内容,如果未能解决你的问题,请参考以下文章