Search in sources :

Example 36 with MessagingException

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);
    }
}
Also used : MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 37 with MessagingException

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);
}
Also used : DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException)

Example 38 with MessagingException

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());
    }
}
Also used : Locale(java.util.Locale) ServiceException(com.zimbra.common.service.ServiceException) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 39 with MessagingException

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());
    }
}
Also used : Locale(java.util.Locale) ServiceException(com.zimbra.common.service.ServiceException) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 40 with MessagingException

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);
}
Also used : ContentDisposition(com.zimbra.common.mime.ContentDisposition) MimeMessage(javax.mail.internet.MimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Aggregations

MessagingException (javax.mail.MessagingException)343 MimeMessage (javax.mail.internet.MimeMessage)135 IOException (java.io.IOException)126 InternetAddress (javax.mail.internet.InternetAddress)70 MimeMultipart (javax.mail.internet.MimeMultipart)64 MimeBodyPart (javax.mail.internet.MimeBodyPart)63 Message (javax.mail.Message)49 Properties (java.util.Properties)45 Session (javax.mail.Session)45 InputStream (java.io.InputStream)43 Date (java.util.Date)34 Address (javax.mail.Address)33 PackageException (com.axway.ats.action.objects.model.PackageException)32 ArrayList (java.util.ArrayList)32 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)31 PublicAtsApi (com.axway.ats.common.PublicAtsApi)31 ServiceException (com.zimbra.common.service.ServiceException)30 ByteArrayInputStream (java.io.ByteArrayInputStream)26 DataHandler (javax.activation.DataHandler)25 UnsupportedEncodingException (java.io.UnsupportedEncodingException)24