use of com.zimbra.client.ZMailbox.ZOutgoingMessage.AttachedMessagePart in project zm-mailbox by Zimbra.
the class ZMailbox method getMessageElement.
public Element getMessageElement(Element req, ZOutgoingMessage message, ZMountpoint mountpoint) {
Element m = req.addElement(MailConstants.E_MSG);
String id = message.getOriginalMessageId();
if (mountpoint != null) {
// Use normalized id for a shared folder
int idx = id.indexOf(":");
if (idx != -1) {
id = id.substring(idx + 1);
}
}
if (id != null) {
m.addAttribute(MailConstants.A_ORIG_ID, id);
}
String msgId = message.getMessageId();
if (msgId != null) {
m.addAttribute(MailConstants.A_ID, msgId);
}
String draftId = message.getDraftMessageId();
if (draftId != null) {
m.addAttribute(MailConstants.A_DRAFT_ID, draftId);
}
if (message.getReplyType() != null) {
m.addAttribute(MailConstants.A_REPLY_TYPE, message.getReplyType());
}
if (message.getAddresses() != null) {
for (ZEmailAddress addr : message.getAddresses()) {
if (mountpoint != null && addr.getType().equals(ZEmailAddress.EMAIL_TYPE_FROM)) {
// For on behalf of messages, replace the from: and add a sender:
Element e = m.addElement(MailConstants.E_EMAIL);
e.addAttribute(MailConstants.A_TYPE, ZEmailAddress.EMAIL_TYPE_SENDER);
e.addAttribute(MailConstants.A_ADDRESS, addr.getAddress());
e = m.addElement(MailConstants.E_EMAIL);
e.addAttribute(MailConstants.A_TYPE, ZEmailAddress.EMAIL_TYPE_FROM);
e.addAttribute(MailConstants.A_ADDRESS, mountpoint.getOwnerDisplayName());
} else {
Element e = m.addElement(MailConstants.E_EMAIL);
e.addAttribute(MailConstants.A_TYPE, addr.getType());
e.addAttribute(MailConstants.A_ADDRESS, addr.getAddress());
e.addAttribute(MailConstants.A_PERSONAL, addr.getPersonal());
}
}
}
if (message.getSubject() != null) {
m.addElement(MailConstants.E_SUBJECT).setText(message.getSubject());
}
if (message.getPriority() != null && message.getPriority().length() != 0) {
m.addAttribute(MailConstants.A_FLAGS, message.getPriority());
}
if (message.getInReplyTo() != null) {
m.addElement(MailConstants.E_IN_REPLY_TO).setText(message.getInReplyTo());
}
if (message.getMessagePart() != null) {
message.getMessagePart().toElement(m);
}
Element attach = null;
if (message.getAttachmentUploadId() != null) {
attach = m.addElement(MailConstants.E_ATTACH);
attach.addAttribute(MailConstants.A_ATTACHMENT_ID, message.getAttachmentUploadId());
}
if (message.getMessageIdsToAttach() != null) {
if (attach == null) {
attach = m.addElement(MailConstants.E_ATTACH);
}
for (String mid : message.getMessageIdsToAttach()) {
attach.addElement(MailConstants.E_MSG).addAttribute(MailConstants.A_ID, mid);
}
}
if (message.getDocIdsToAttach() != null) {
if (attach == null) {
attach = m.addElement(MailConstants.E_ATTACH);
}
for (String did : message.getDocIdsToAttach()) {
attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, did);
}
}
if (message.getContactIdsToAttach() != null) {
if (attach == null)
attach = m.addElement(MailConstants.E_ATTACH);
for (String cid : message.getContactIdsToAttach()) {
attach.addElement(MailConstants.E_CONTACT).addAttribute(MailConstants.A_ID, cid);
}
}
if (message.getMessagePartsToAttach() != null) {
if (attach == null) {
attach = m.addElement(MailConstants.E_ATTACH);
}
for (AttachedMessagePart part : message.getMessagePartsToAttach()) {
if (part.getContentId() == null || part.getContentId().equals("")) {
attach.addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_MESSAGE_ID, part.getMessageId()).addAttribute(MailConstants.A_PART, part.getPartName());
}
}
}
return m;
}
use of com.zimbra.client.ZMailbox.ZOutgoingMessage.AttachedMessagePart in project zm-mailbox by Zimbra.
the class TestMaxMessageSize method testMaxMessageSizeSaveDraft.
/**
* Confirms that
* @throws Exception
*/
@Test
public void testMaxMessageSizeSaveDraft() throws Exception {
setMaxMessageSize(TEST_MAX_MESSAGE_SIZE);
// Upload attachment whose size is 50% of the threshold. If this number
// gets incremented twice, it would exceed the threshold.
Map<String, byte[]> attachments = new HashMap<String, byte[]>();
attachments.put("file1.exe", new byte[(int) (TEST_MAX_MESSAGE_SIZE * 0.5)]);
ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
String aid = mbox.uploadAttachments(attachments, 5000);
// Save draft
ZOutgoingMessage outgoing = new ZOutgoingMessage();
List<ZEmailAddress> addresses = new ArrayList<ZEmailAddress>();
addresses.add(new ZEmailAddress(TestUtil.getAddress(USER_NAME), null, null, ZEmailAddress.EMAIL_TYPE_TO));
outgoing.setAddresses(addresses);
outgoing.setAttachmentUploadId(aid);
String subject = NAME_PREFIX + "testMaxMessageSizeSaveDraft";
outgoing.setSubject(subject);
ZMessage draft = mbox.saveDraft(outgoing, null, null);
// Send the draft
outgoing.setAttachmentUploadId(null);
List<AttachedMessagePart> attachedParts = new ArrayList<AttachedMessagePart>();
attachedParts.add(new AttachedMessagePart(draft.getId(), "1", null));
outgoing.setMessagePartsToAttach(attachedParts);
mbox.sendMessage(outgoing, null, false);
TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
// Reduce max message size and confirm that the send fails.
setMaxMessageSize((int) (TEST_MAX_MESSAGE_SIZE * 0.6));
try {
mbox.sendMessage(outgoing, null, false);
Assert.fail("Message send should not have succeeded.");
} catch (SoapFaultException e) {
// Message send was not allowed, as expected.
validateMessageTooBigFault(e);
}
}
use of com.zimbra.client.ZMailbox.ZOutgoingMessage.AttachedMessagePart in project zm-mailbox by Zimbra.
the class TestSendAndReceive method testMalformedContentType.
/**
* Confirms that we can forward attachments with a malformed content type (bug 42452).
*/
@Test
public void testMalformedContentType() throws Exception {
TestUtil.createAccount(USER_NAME);
// Generate original message.
String subject = NAME_PREFIX + " testMalformedContentType";
MessageBuilder builder = new MessageBuilder().withFrom(USER_NAME).withToRecipient(USER_NAME).withSubject(subject).withAttachment("This is an attachment", "test.txt", MimeConstants.CT_TEXT_PLAIN);
// Hack Content-Type so that it's invalid.
BufferedReader reader = new BufferedReader(new StringReader(builder.create()));
StringBuilder msgBuf = new StringBuilder();
String line = reader.readLine();
boolean replaced = false;
while (line != null) {
if (line.matches("Content-Type.*test.txt.*")) {
line = line.replace("Content-Type: text/plain;", "Content-Type: text/plain;;");
Assert.assertTrue("Unexpected line: " + line, line.contains(";;"));
replaced = true;
}
msgBuf.append(line).append("\r\n");
line = reader.readLine();
}
Assert.assertTrue("Could not find text/plain attachment.", replaced);
// Add message to the mailbox.
ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, msgBuf.toString());
// Forward the attachment in a new message.
ZMessage srcMsg = TestUtil.getMessage(mbox, "subject:\"" + subject + "\"");
ZMimePart srcAttachPart = srcMsg.getMimeStructure().getChildren().get(1);
Assert.assertEquals("test.txt", srcAttachPart.getFileName());
ZOutgoingMessage outgoing = new ZOutgoingMessage();
outgoing.setMessagePart(new MessagePart(MimeConstants.CT_TEXT_PLAIN, "Forwarding attachment."));
outgoing.setMessagePartsToAttach(Arrays.asList(new AttachedMessagePart(srcMsg.getId(), srcAttachPart.getPartName(), null)));
String address = TestUtil.getAddress(USER_NAME);
ZEmailAddress sender = new ZEmailAddress(address, null, null, ZEmailAddress.EMAIL_TYPE_FROM);
ZEmailAddress recipient = new ZEmailAddress(address, null, null, ZEmailAddress.EMAIL_TYPE_TO);
outgoing.setAddresses(Arrays.asList(sender, recipient));
String fwdSubject = NAME_PREFIX + " testMalformedContentType forward";
outgoing.setSubject(fwdSubject);
mbox.sendMessage(outgoing, null, false);
// Make sure the forwarded message arrives.
ZMessage fwd = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + fwdSubject + "\"");
ZMimePart fwdAttachPart = fwd.getMimeStructure().getChildren().get(1);
Assert.assertEquals("test.txt", fwdAttachPart.getFileName());
}
use of com.zimbra.client.ZMailbox.ZOutgoingMessage.AttachedMessagePart in project zm-mailbox by Zimbra.
the class TestSendAndReceive method testInlineAttachment.
/**
* Test inline attachment. See bug #88933
*/
@Test
public void testInlineAttachment() throws Exception {
TestUtil.createAccount(USER_NAME);
ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
//create inline image attachment
String imgName = "synacore.jpg";
String content = "<content><img src=\"cid:" + imgName + "\"></content>";
List<AttachedMessagePart> inlineAttachments = new ArrayList<AttachedMessagePart>();
String attachId = mbox.uploadAttachment(imgName, new byte[1024], MimeConstants.CT_IMAGE_JPEG, 5000);
AttachedMessagePart inlineImg = new AttachedMessagePart(attachId, imgName);
inlineAttachments.add(inlineImg);
MessagePart html = new MessagePart(MimeConstants.CT_TEXT_HTML, content, inlineAttachments);
//create outgoing message
String subject = NAME_PREFIX + " testInlineAttachment " + MimeConstants.CT_TEXT_PLAIN + " 1";
ZOutgoingMessage outgoingMsg = new ZOutgoingMessage();
List<ZEmailAddress> addresses = new ArrayList<ZEmailAddress>();
addresses.add(new ZEmailAddress(TestUtil.addDomainIfNecessary(USER_NAME), null, null, ZEmailAddress.EMAIL_TYPE_TO));
outgoingMsg.setAddresses(addresses);
outgoingMsg.setSubject(subject);
MessagePart related = new MessagePart(MimeConstants.CT_MULTIPART_RELATED, html);
outgoingMsg.setMessagePart(related);
mbox.sendMessage(outgoingMsg, null, false);
//verify that received message has the attachment
ZMessage incoming = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
Assert.assertTrue("this message should have an attachment", incoming.hasAttachment());
Assert.assertNotNull("this message should have mime parts", incoming.getMimeStructure());
Assert.assertNotNull("this message should have two subparts", incoming.getMimeStructure().getChildren());
List<ZMimePart> parts = incoming.getMimeStructure().getChildren();
Assert.assertEquals("this message should have 2 subparts", 2, parts.size());
Assert.assertTrue("one of the parts should have a content id", parts.get(0).getContentId() != null || parts.get(1).getContentId() != null);
Assert.assertTrue("one of the parts should be " + imgName, imgName.equalsIgnoreCase(parts.get(0).getFileName()) || imgName.equalsIgnoreCase(parts.get(1).getFileName()));
}
use of com.zimbra.client.ZMailbox.ZOutgoingMessage.AttachedMessagePart in project zm-mailbox by Zimbra.
the class TestUtil method saveDraftAndSendMessage.
public static void saveDraftAndSendMessage(ZMailbox senderMbox, String recipient, String subject, String body, String attachmentUploadId) throws ServiceException {
ZOutgoingMessage outgoingDraft = getOutgoingMessage(recipient, subject, body, attachmentUploadId);
ZMessage draft = senderMbox.saveDraft(outgoingDraft, null, Integer.toString(Mailbox.ID_FOLDER_DRAFTS));
ZOutgoingMessage outgoing = getOutgoingMessage(recipient, subject, body, null);
if (attachmentUploadId != null) {
AttachedMessagePart part = new AttachedMessagePart(draft.getId(), "2", null);
outgoing.setMessagePartsToAttach(Arrays.asList(part));
}
senderMbox.sendMessage(outgoing, null, false);
}
Aggregations