用于发送多个文档的JMS字节消息

用于发送多个文档的JMS字节消息,jms,attachment,Jms,Attachment,我有一个批量作业的要求,应该发送电子邮件。但是,我必须打破这种方式,我需要移交电子邮件的详细信息,通过另一个系统,然后将阅读放置的邮件和发送电子邮件悠闲 我的电子邮件有附件。我如何做到这一点?给定电子邮件可能有多个附件 我读过关于bytes的邮件,但如何将其用于给定电子邮件的多个附件 对此有何想法?您可以发送一条包含电子邮件详细信息(从、到列表、主题、文本)的JMS消息,然后以JMS字节消息的形式发送附件,每个附件消息具有相同的自定义标识符 发送方 // create JMS Bytes mes

我有一个批量作业的要求,应该发送电子邮件。但是,我必须打破这种方式,我需要移交电子邮件的详细信息,通过另一个系统,然后将阅读放置的邮件和发送电子邮件悠闲

我的电子邮件有附件。我如何做到这一点?给定电子邮件可能有多个附件

我读过关于bytes的邮件,但如何将其用于给定电子邮件的多个附件


对此有何想法?

您可以发送一条包含电子邮件详细信息(从、到列表、主题、文本)的JMS消息,然后以JMS字节消息的形式发送附件,每个附件消息具有相同的自定义标识符

发送方

// create JMS Bytes message with mail content
// MailData class should implement java.io.Serializable)
MailData mailData = new MailData();
// emailID could be GUID or anything else that would uniquely identify mail
mailData.setEmailID(emailID);
mailData.setFrom(from);
mailData.setToList(toList);
mailData.setSubject(subject);
mailData.setText(text);

BytesMessage msg = session.createBytesMessage();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(mailData);
out.close();
bos.close();
msg.writeBytes(bos.toByteArray());
producer.send(msg);

// for the sake of simplicity, object attachment contains attachment name, MIME type
// and value as byte array
for(Attachment att : attachmentList) {
    BytesMessage msgAtt = session.createBytesMessage();
    // emailID
    msgAtt.setStringProperty("emailId", emailID);
    msgAtt.setStringProperty("fileName", att.getAttachmentName());
    msgAtt.setStringProperty("mimeType", att.getMimeType());
    // set bytes message payload to attachment content
    msgAtt.writeBytes(att.getValue());
    producer.send(msgAtt);
}
接收端

BytesMessage bytesMessage = (BytesMessage) message;
byte[] bytes = new byte[(int) bytesMessage.getBodyLength()];
bytesMessage.readBytes(bytes);
ByteArrayInputStream bis = new ByteArrayInputStream(bytesMessage);
ObjectInputStream in = new ObjectInputStream(bis);
MailData mailData = (MailData) in.readObject();
in.close();
bis.close();

// get data from queue with the same emailID
MessageConsumer consumer = session.createConsumer(queue, "emailID='"
                + mailData.getEmailID() + "'");
connection.start();
Message attMsg = null;
while((attMsg = consumer.receiveNoWait()) != null) {
    String fileName = attMsg.getStringProperty("fileName");
    String mimeType = att.getStringProperty("mimeType");
    BytesMessage bytesMessage = (BytesMessage) attMsg;
    byte[] attachmentValue = new byte[(int) bytesMessage.getBodyLength()];
    bytesMessage.readBytes(attachmentValue);
}