use of javax.mail.MessagingException in project zm-mailbox by Zimbra.
the class JavaMailMimeMessage method setContent.
@Override
public void setContent(Object o, String type) throws MessagingException {
if (ZPARSER) {
com.zimbra.common.mime.ContentType ctype = new com.zimbra.common.mime.ContentType(type);
if (o instanceof JavaMailMimeMultipart) {
setContent((Multipart) o);
setHeader("Content-Type", type);
} else if (o instanceof JavaMailMimeMessage) {
zmessage.setBodyPart(((JavaMailMimeMessage) o).getZimbraMimeMessage());
setHeader("Content-Type", type);
jmcontent = o;
} else if (o instanceof String) {
if (ctype.getPrimaryType().equals("text")) {
setText((String) o, ctype.getParameter("charset"), ctype.getSubType());
setHeader("Content-Type", type);
} else {
try {
setDataSource(new ByteArrayDataSource((String) o, type));
} catch (IOException ioe) {
throw new MessagingException("error setting string content", ioe);
}
}
} else {
setDataHandler(new DataHandler(o, type));
}
} else {
super.setContent(o, type);
}
}
use of javax.mail.MessagingException in project zm-mailbox by Zimbra.
the class ZMimeBodyPart method recalculateTransferEncoding.
static void recalculateTransferEncoding(ZMimePart part) throws MessagingException {
// don't overwrite an explicitly-set transfer encoding
if (part.getHeader("Content-Transfer-Encoding") != null)
return;
// don't set transfer encoding on messages or multiparts
DataHandler dh = part.getDataHandler();
ZContentType ctype = new ZContentType(dh.getContentType());
if (ctype.getBaseType().equals("message/rfc822") || ctype.getPrimaryType().equals("multipart"))
return;
boolean isText = ctype.getPrimaryType().equals("text");
String disp = part.getDisposition();
boolean isAttachment = disp != null && disp.equals(ATTACHMENT);
String encoding = "base64";
if (dh.getName() == null) {
try {
EncodingOutputStream eos = new EncodingOutputStream();
dh.writeTo(eos);
encoding = eos.tester.getEncoding(isAttachment, isText);
} catch (Exception e) {
}
} else {
EncodingInputStream eis = null;
try {
eis = new EncodingInputStream(dh.getDataSource().getInputStream());
ByteUtil.drain(eis, false);
encoding = eis.tester.getEncoding(isAttachment, isText);
} catch (IOException e) {
} finally {
ByteUtil.closeStream(eis);
}
}
part.setHeader("Content-Transfer-Encoding", encoding);
}
use of javax.mail.MessagingException in project zm-mailbox by Zimbra.
the class ExtShareInfoTest method testGenNotifyBody.
@Test
public void testGenNotifyBody() {
Locale locale = new Locale("en", "US");
String notes = "none";
ShareInfoData sid = new ShareInfoData();
sid.setGranteeDisplayName("Demo User Three");
sid.setGranteeId(testAcct.getId());
sid.setGranteeName(testAcct.getName());
sid.setGranteeType(ACL.GRANTEE_GUEST);
sid.setPath("/Inbox/Test");
sid.setFolderDefaultView(MailItem.Type.MESSAGE);
sid.setItemUuid("9badf685-3420-458b-9ce5-826b0bec638f");
sid.setItemId(257);
sid.setOwnerAcctId(ownerAcct.getId());
sid.setOwnerAcctEmail(ownerAcct.getName());
sid.setOwnerAcctDisplayName("Demo User Two");
try {
sid.setRights(ACL.stringToRights("rwidxap"));
MimeMultipart mmp = ShareInfo.NotificationSender.genNotifBody(sid, notes, locale, null, null);
Assert.assertNotNull(mmp);
String body = (String) mmp.getBodyPart(0).getDataHandler().getContent();
int index = body.indexOf("http");
int endIndex = body.indexOf(".", index);
String authUrl = body.substring(index, endIndex);
index = authUrl.indexOf("p=");
String authToken = authUrl.substring(index + 2);
try {
ZimbraAuthToken.getAuthToken(authToken);
fail("Authtoken should fail");
} catch (AuthTokenException e) {
assertTrue(e.getMessage().contains("hmac failure"));
}
// Commenting for now, need to figure out why it fails on hudson
// try {
// ExternalUserProvServlet.validatePrelimToken(authToken);
// } catch (Exception e) {
// fail("Should not throw Exception" + e.getMessage());
// }
} catch (ServiceException | MessagingException | IOException e) {
fail("Exception should not be thrown: " + e.getMessage());
}
}
use of javax.mail.MessagingException in project zm-mailbox by Zimbra.
the class ShareInfoTest method testGenNotifyBody.
@Test
public void testGenNotifyBody() {
Locale locale = new Locale("en", "US");
String notes = "none";
ShareInfoData sid = new ShareInfoData();
sid.setGranteeDisplayName("Demo User Three");
sid.setGranteeId("46031e4c-deb4-4724-b5bb-8f854d0c518a");
sid.setGranteeName("Demo User Three");
sid.setGranteeType(ACL.GRANTEE_USER);
sid.setPath("/Calendar/Cal1");
sid.setFolderDefaultView(MailItem.Type.APPOINTMENT);
sid.setItemUuid("9badf685-3420-458b-9ce5-826b0bec638f");
sid.setItemId(257);
sid.setOwnerAcctId("bbf152ca-e7cd-477e-9f72-70fef715c5f9");
sid.setOwnerAcctEmail("test@zimbra.com");
sid.setOwnerAcctDisplayName("Demo User Two");
try {
sid.setRights(ACL.stringToRights("rwidxap"));
MimeMultipart mmp = ShareInfo.NotificationSender.genNotifBody(sid, notes, locale, null, null);
Assert.assertNotNull(mmp);
String body = (String) mmp.getBodyPart(0).getDataHandler().getContent();
assertTrue(body.indexOf("Role: Admin") != -1);
} catch (ServiceException | MessagingException | IOException e) {
fail("Exception should not be thrown: " + e.getMessage());
}
}
use of javax.mail.MessagingException in project zm-mailbox by Zimbra.
the class MailboxTestUtil method generateMessageWithAttachment.
public static ParsedMessage generateMessageWithAttachment(String subject) throws Exception {
MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
mm.setHeader("From", "Vera Oliphant <oli@example.com>");
mm.setHeader("To", "Jimmy Dean <jdean@example.com>");
mm.setHeader("Subject", subject);
mm.setText("Good as gold");
MimeMultipart multi = new ZMimeMultipart("mixed");
ContentDisposition cdisp = new ContentDisposition(Part.ATTACHMENT);
cdisp.setParameter("filename", "fun.txt");
ZMimeBodyPart bp = new ZMimeBodyPart();
// it gets called before setting Content-Type and CTE headers.
try {
bp.setDataHandler(new DataHandler(new ByteArrayDataSource("Feeling attached.", "text/plain")));
} catch (IOException e) {
throw new MessagingException("could not generate mime part content", e);
}
bp.addHeader("Content-Disposition", cdisp.toString());
bp.addHeader("Content-Type", "text/plain");
bp.addHeader("Content-Transfer-Encoding", MimeConstants.ET_8BIT);
multi.addBodyPart(bp);
mm.setContent(multi);
mm.saveChanges();
return new ParsedMessage(mm, false);
}
Aggregations