use of javax.mail.internet.MimePart in project zm-mailbox by Zimbra.
the class ContactTest method modifyContactHavingAttachment.
/**
* Modify Contact having an attachment (bug 70488).
*/
@Test
public void modifyContactHavingAttachment() throws Exception {
// Create a contact with an attachment.
Map<String, String> attrs = new HashMap<String, String>();
attrs.put("fullName", "Contact Initial Content");
byte[] attachData = "attachment 1".getBytes();
Attachment textAttachment = new Attachment(attachData, "text/plain", "customField", "file.txt");
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
Contact contact = mbox.createContact(null, new ParsedContact(attrs, Lists.newArrayList(textAttachment)), Mailbox.ID_FOLDER_CONTACTS, null);
ParsedContact pc = new ParsedContact(contact).modify(new ParsedContact.FieldDeltaList(), new ArrayList<Attachment>());
MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), pc.getContentStream());
MimePart mp = Mime.getMimePart(mm, "1");
Assert.assertEquals("text/plain", mp.getContentType());
Assert.assertEquals("attachment 1", mp.getContent());
}
use of javax.mail.internet.MimePart in project zm-mailbox by Zimbra.
the class MimeTest method fileAsStream.
private void fileAsStream(String extension, boolean expectText) throws MessagingException, IOException {
if (extension.charAt(0) == '.') {
extension = extension.substring(1);
}
String content = "From: user1@example.com\r\n" + "To: user2@example.com\r\n" + "Subject: test\r\n" + "Content-Type: application/octet-stream;name=\"test." + extension + "\"\r\n" + "Content-Transfer-Encoding: base64\r\n\r\n" + //obviously not a real file
"R0a1231312ad124svsdsal==";
MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
MimePart part = Mime.getMimePart(mm, "1");
String expectedType = expectText ? "text/plain" : "application/octet-stream";
Assert.assertEquals(expectedType, Mime.getContentType(part.getContentType()));
}
use of javax.mail.internet.MimePart in project ats-framework by Axway.
the class MimePackage method getRegularPartCharset.
/**
* Get the character set of a regular part
*
* @param partIndex
* the index of the part
* @return the charset
* @throws PackageException
*/
@PublicAtsApi
public String getRegularPartCharset(int partIndex) throws PackageException {
// first check if there is part at this position at all
if (partIndex >= regularPartIndices.size()) {
throw new NoSuchMimePartException("No regular part at position '" + partIndex + "'");
}
try {
MimePart part = getPart(regularPartIndices.get(partIndex));
// get the content type header
ContentType contentType = new ContentType(part.getContentType());
return contentType.getParameter("charset");
} catch (MessagingException me) {
throw new PackageException(me);
}
}
use of javax.mail.internet.MimePart in project ats-framework by Axway.
the class MimePackage method getAllStreams.
@PublicAtsApi
public List<InputStream> getAllStreams() throws PackageException {
boolean storeReconnected = false;
try {
// store should be opened for actions including getting InputStream.
storeReconnected = reconnectStoreIfClosed();
ArrayList<InputStream> streams = new ArrayList<InputStream>();
try {
for (MimePart part : parts) {
streams.add(part.getInputStream());
}
} finally {
closeStoreConnection(storeReconnected);
}
return streams;
} catch (MessagingException me) {
throw new PackageException("Could not read mime parts", me);
} catch (IOException ioe) {
throw new PackageException("Could not read mime parts", ioe);
}
}
use of javax.mail.internet.MimePart in project ats-framework by Axway.
the class MimePackage method setAttachmentFileName.
/**
* Set/modify attachment file name
*
* @param attachmentPartIndex index among attachment parts only
* @param fileName the file name. Add one or reset existing one
* @throws NoSuchMimePartException if not such attachment part is found
* @throws PackageException in case of other mail messaging exception
*/
@PublicAtsApi
public void setAttachmentFileName(int attachmentPartIndex, String fileName) throws PackageException {
// first check if there is part at this position at all
if (attachmentPartIndex >= attachmentPartIndices.size()) {
throw new NoSuchMimePartException("No attachment at position '" + attachmentPartIndex + "'");
}
try {
MimePart part = getPart(attachmentPartIndices.get(attachmentPartIndex));
// set the attachment file name
part.setFileName(fileName);
// must save now
this.message.saveChanges();
} catch (MessagingException me) {
throw new PackageException(me);
}
}
Aggregations