use of javax.mail.internet.MimeMultipart in project zm-mailbox by Zimbra.
the class ParseMimeMessageTest method staleReference.
@Test
public void staleReference() throws Exception {
Account acct = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
// first, create the original draft
OperationContext octxt = new OperationContext(acct);
Document doc = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc", MimeConstants.CT_TEXT_PLAIN, null, null, randomContent("test1", 8192));
Document doc2 = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc2", MimeConstants.CT_TEXT_PLAIN, null, null, randomContent("test2", 8192));
Element el = new Element.JSONElement(MailConstants.E_MSG), attach;
el.addAttribute(MailConstants.E_SUBJECT, "has attachment");
el.addElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, "rcpt@zimbra.com");
el.addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, "This is the content.");
attach = el.addElement(MailConstants.E_ATTACH);
attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc.getId());
attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc2.getId());
ZimbraSoapContext zsc = getMockSoapContext();
MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
Message draft = mbox.saveDraft(octxt, new ParsedMessage(mm, false), -1);
// then, create a new draft that references one of the original draft's attachments
attach.detach();
(attach = el.addElement(MailConstants.E_ATTACH)).addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_MESSAGE_ID, draft.getId()).addAttribute(MailConstants.A_PART, "3");
mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
// delete the draft itself and then try to save the new draft
mbox.delete(octxt, draft.getId(), MailItem.Type.MESSAGE);
Message draft2 = mbox.saveDraft(octxt, new ParsedMessage(mm, false), -1);
// check that the attachment's content is present and correct
MimeMultipart multi = (MimeMultipart) (draft2.getMimeMessage().getContent());
Assert.assertEquals("2 parts in draft", 2, multi.getCount());
Assert.assertEquals("attached part content", "test2", firstLine((MimeBodyPart) multi.getBodyPart(1)));
}
use of javax.mail.internet.MimeMultipart in project zm-mailbox by Zimbra.
the class SendShareNotificationTest method generateShareNotification.
@Override
protected MimeMessage generateShareNotification(Account authAccount, Account ownerAccount, ShareInfoData sid, String notes, SendShareNotificationRequest.Action action, Collection<String> internalRecipients, String externalRecipient) throws ServiceException, MessagingException {
MimeMessage mm = super.generateShareNotification(authAccount, ownerAccount, sid, notes, action, internalRecipients, externalRecipient);
try {
MimeMultipart mmp = (MimeMultipart) mm.getContent();
BodyPart bp = mmp.getBodyPart(2);
Assert.assertTrue(bp.getContentType().startsWith(MimeConstants.CT_XML_ZIMBRA_SHARE));
Element share = Element.parseXML(bp.getInputStream());
if (revoke) {
Assert.assertEquals(share.getQName().getName(), MailConstants.ShareConstants.E_REVOKE);
} else {
Assert.assertEquals(share.getQName().getName(), MailConstants.ShareConstants.E_SHARE);
}
} catch (Exception e) {
Assert.fail(e.getMessage());
}
return mm;
}
use of javax.mail.internet.MimeMultipart in project perun by CESNET.
the class MessagePreparator method prepare.
/*
* (non-Javadoc)
*
* @see org.springframework.mail.javamail.MimeMessagePreparator#prepare(javax.mail.internet.MimeMessage)
*/
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMultipart mpRoot = new MimeMultipart("mixed");
Multipart mp = new MimeMultipart("alternative");
// Create a body part to house the multipart/alternative Part
MimeBodyPart contentPartRoot = new MimeBodyPart();
contentPartRoot.setContent(mp);
// Add the root body part to the root multipart
mpRoot.addBodyPart(contentPartRoot);
// adding recipients, cc and bcc
if (getTo() != null) {
for (String to : getTo()) {
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
}
}
if (getCc() != null) {
for (String cc : getCc()) {
mimeMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
}
}
if (getBcc() != null) {
for (String bcc : getBcc()) {
mimeMessage.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
}
}
// adding from and subject
mimeMessage.setFrom(new InternetAddress(getFrom(), getFromText()));
mimeMessage.setSubject(getSubject());
if (getEmailType().equals(EmailType.HTML)) {
mp.addBodyPart(createHtmlMessage());
} else if (getEmailType().equals(EmailType.PLAIN)) {
mp.addBodyPart(createTextMessage());
}
// Create an "ATTACHMENT" - we must put it to mpRoot(mixed content)
if (getFileNames() != null) {
for (String filename : getFileNames()) {
mpRoot.addBodyPart(createAttachment(filename));
}
}
mimeMessage.setContent(mpRoot);
logger.debug("Message is prepared to send");
}
use of javax.mail.internet.MimeMultipart in project perun by CESNET.
the class MessagePreparator method createHtmlMessage.
/**
* Creates HTML message from freemarker template
*
* @return
* @throws Exception
*/
private BodyPart createHtmlMessage() throws Exception {
Multipart htmlContent = new MimeMultipart("related");
BodyPart htmlPage = new MimeBodyPart();
htmlPage.setDataHandler(createDataHandler(content.getBytes("utf-8"), "text/html;charset=utf-8"));
htmlContent.addBodyPart(htmlPage);
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlContent);
logger.debug("HTML MESSAGE CREATED ; content: " + content);
return htmlPart;
}
use of javax.mail.internet.MimeMultipart in project ats-framework by Axway.
the class MimePackage method addAlternativePart.
/**
* Add a multipart/alternative part to message body
*
* @param plainContent
* the content of the text/plain sub-part
* @param htmlContent
* the content of the text/html sub-part
* @param charset
* the character set for the part
* @throws PackageException
*/
@PublicAtsApi
public void addAlternativePart(String plainContent, String htmlContent, String charset) throws PackageException {
MimeMultipart alternativePart = new MimeMultipart("alternative");
try {
// create a new text/plain part
MimeBodyPart plainPart = new MimeBodyPart();
plainPart.setText(plainContent, charset, PART_TYPE_TEXT_PLAIN);
plainPart.setDisposition(MimeBodyPart.INLINE);
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setText(htmlContent, charset, PART_TYPE_TEXT_HTML);
htmlPart.setDisposition(MimeBodyPart.INLINE);
alternativePart.addBodyPart(plainPart, 0);
alternativePart.addBodyPart(htmlPart, 1);
MimeBodyPart mimePart = new MimeBodyPart();
mimePart.setContent(alternativePart);
addPart(mimePart, PART_POSITION_LAST);
} catch (MessagingException me) {
throw new PackageException(me);
}
}
Aggregations