Search in sources :

Example 96 with MessagingException

use of javax.mail.MessagingException in project ats-framework by Axway.

the class MimePackage method getAttachmentFileName.

/**
     * Get an attachment's file name
     *
     * @param partIndex
     * @return
     * @throws PackageException
     */
@PublicAtsApi
public String getAttachmentFileName(int partIndex) throws PackageException {
    // first check if there is part at this position at all
    if (partIndex >= attachmentPartIndices.size()) {
        throw new NoSuchMimePartException("No attachment at position '" + partIndex + "'");
    }
    try {
        MimePart part = getPart(attachmentPartIndices.get(partIndex));
        // get the attachment file name
        String fileName = part.getFileName();
        if (fileName == null) {
            throw new PackageException("Could not determine file name for attachment at position " + partIndex);
        }
        return fileName;
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : MessagingException(javax.mail.MessagingException) NoSuchMimePartException(com.axway.ats.action.objects.model.NoSuchMimePartException) MimePart(javax.mail.internet.MimePart) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 97 with MessagingException

use of javax.mail.MessagingException in project ats-framework by Axway.

the class ImapFolder method open.

/**
     *
     * @see com.axway.ats.rbv.storage.Matchable#open()
     */
public void open() throws RbvStorageException {
    //first check if the folder is already open
    if (isOpen) {
        throw new MatchableAlreadyOpenException(getDescription() + " is already open");
    }
    try {
        isInitialPass = true;
        // create and connect to the user's imap folder
        if (!store.isConnected()) {
            store.connect(serverHost, userName, password);
            log.debug("Connected to store '" + serverHost + "' with user '" + userName + "' and password '" + password + "'");
        }
        if (folder == null || !folder.isOpen()) {
            folder = store.getFolder(folderName);
            folder.open(Folder.READ_WRITE);
        }
        allMetaDataList.clear();
        newMetaDataList.clear();
        isOpen = true;
        log.info("Opened " + getDescription());
    } catch (MessagingException me) {
        throw new RbvStorageException("Could not open " + getDescription(), me);
    }
}
Also used : MatchableAlreadyOpenException(com.axway.ats.rbv.model.MatchableAlreadyOpenException) MessagingException(javax.mail.MessagingException) RbvStorageException(com.axway.ats.rbv.model.RbvStorageException)

Example 98 with MessagingException

use of javax.mail.MessagingException in project zm-mailbox by Zimbra.

the class FilterUtil method redirect.

public static void redirect(OperationContext octxt, Mailbox sourceMbox, MimeMessage msg, String destinationAddress) throws ServiceException {
    MimeMessage outgoingMsg;
    try {
        if (!isMailLoop(sourceMbox, msg, new String[] { HEADER_FORWARDED })) {
            outgoingMsg = new Mime.FixedMimeMessage(msg);
            Mime.recursiveRepairTransferEncoding(outgoingMsg);
            outgoingMsg.addHeader(HEADER_FORWARDED, sourceMbox.getAccount().getName());
            outgoingMsg.saveChanges();
        } else {
            String error = String.format("Detected a mail loop for message %s.", Mime.getMessageID(msg));
            throw ServiceException.FAILURE(error, null);
        }
    } catch (MessagingException e) {
        try {
            outgoingMsg = createRedirectMsgOnError(msg);
            ZimbraLog.filter.info("Message format error detected.  Wrapper class in use.  %s", e.toString());
        } catch (MessagingException again) {
            throw ServiceException.FAILURE("Message format error detected.  Workaround failed.", again);
        }
    } catch (IOException e) {
        try {
            outgoingMsg = createRedirectMsgOnError(msg);
            ZimbraLog.filter.info("Message format error detected.  Wrapper class in use.  %s", e.toString());
        } catch (MessagingException me) {
            throw ServiceException.FAILURE("Message format error detected.  Workaround failed.", me);
        }
    }
    MailSender sender = sourceMbox.getMailSender().setSaveToSent(false).setRedirectMode(true).setSkipHeaderUpdate(true);
    try {
        if (Provisioning.getInstance().getLocalServer().isMailRedirectSetEnvelopeSender()) {
            if (isDeliveryStatusNotification(msg) && LC.filter_null_env_sender_for_dsn_redirect.booleanValue()) {
                sender.setEnvelopeFrom("<>");
                sender.setDsnNotifyOptions(MailSender.DsnNotifyOption.NEVER);
            } else {
                // Set envelope sender to the account name (bug 31309).
                Account account = sourceMbox.getAccount();
                sender.setEnvelopeFrom(account.getName());
            }
        } else {
            Address from = ArrayUtil.getFirstElement(outgoingMsg.getFrom());
            if (from != null) {
                String address = ((InternetAddress) from).getAddress();
                sender.setEnvelopeFrom(address);
            }
        }
        sender.setRecipients(destinationAddress);
        sender.sendMimeMessage(octxt, sourceMbox, outgoingMsg);
    } catch (MessagingException e) {
        ZimbraLog.filter.warn("Envelope sender will be set to the default value.", e);
    }
}
Also used : Mime(com.zimbra.cs.mime.Mime) Account(com.zimbra.cs.account.Account) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MailSender(com.zimbra.cs.mailbox.MailSender) IOException(java.io.IOException)

Example 99 with MessagingException

use of javax.mail.MessagingException in project zm-mailbox by Zimbra.

the class FilterUtil method reject.

public static void reject(OperationContext octxt, Mailbox mailbox, ParsedMessage parsedMessage, String reason, LmtpEnvelope envelope) throws MessagingException, ServiceException {
    MimeMessage mimeMessage = parsedMessage.getMimeMessage();
    if (isMailLoop(mailbox, mimeMessage, new String[] { HEADER_FORWARDED })) {
        // Detected a mail loop.  Do not send MDN, but just discard the message
        String error = String.format("Detected a mail loop for message %s. No MDN sent.", Mime.getMessageID(mimeMessage));
        ZimbraLog.filter.info(error);
        throw ServiceException.FAILURE(error, null);
    }
    String reportTo = null;
    if (envelope != null && envelope.hasSender()) {
        reportTo = envelope.getSender().getEmailAddress();
    }
    if (reportTo == null || reportTo.isEmpty()) {
        String[] returnPath = mimeMessage.getHeader(HEADER_RETURN_PATH);
        if (returnPath == null || returnPath.length == 0) {
            // >> NOT be generated if the MAIL FROM (or Return-Path) is empty.
            throw new MessagingException("Neither 'envelope from' nor 'Return-Path' specified. Can't locate the address to reject to (No MDN sent)");
        } else {
            // At least one 'return-path' should exist.
            reportTo = returnPath[0];
        }
    }
    Account owner = mailbox.getAccount();
    Locale locale = owner.getLocale();
    String charset = owner.getPrefMailDefaultCharset();
    if (charset == null) {
        charset = MimeConstants.P_CHARSET_UTF8;
    }
    SMTPMessage report = new SMTPMessage(JMSession.getSmtpSession());
    // add the forwarded header account names to detect the mail loop between accounts
    for (String headerFwdAccountName : Mime.getHeaders(mimeMessage, HEADER_FORWARDED)) {
        report.addHeader(HEADER_FORWARDED, headerFwdAccountName);
    }
    report.addHeader(HEADER_FORWARDED, owner.getName());
    // MDN header
    report.setEnvelopeFrom("<>");
    report.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(reportTo));
    String subject = L10nUtil.getMessage(MsgKey.seiveRejectMDNSubject, locale);
    report.setSubject(subject);
    report.setSentDate(new Date());
    InternetAddress address = new JavaMailInternetAddress(owner.getName());
    report.setFrom(address);
    MimeMultipart multi = new ZMimeMultipart("report");
    // part 1: human-readable notification
    String text = L10nUtil.getMessage(MsgKey.seiveRejectMDNErrorMsg, locale) + "\n" + reason;
    MimeBodyPart mpText = new ZMimeBodyPart();
    mpText.setText(text, CharsetUtil.checkCharset(text, charset));
    multi.addBodyPart(mpText);
    // part 2: disposition notification
    StringBuilder mdn = new StringBuilder();
    mdn.append("Final-Recipient: rfc822;").append(owner.getName()).append("\r\n");
    mdn.append("Disposition: automatic-action/MDN-sent-automatically");
    mdn.append("; deleted\r\n");
    MimeBodyPart mpMDN = new ZMimeBodyPart();
    mpMDN.setText(mdn.toString(), MimeConstants.P_CHARSET_UTF8);
    mpMDN.setHeader("Content-Type", "message/disposition-notification; charset=utf-8");
    multi.addBodyPart(mpMDN);
    // Assemble the MDN
    report.setContent(multi);
    report.setHeader("Content-Type", multi.getContentType() + "; report-type=disposition-notification");
    report.saveChanges();
    MailSender mailSender = mailbox.getMailSender().setSaveToSent(false);
    mailSender.setRecipients(reportTo);
    mailSender.setEnvelopeFrom("<>");
    mailSender.sendMimeMessage(octxt, mailbox, report);
}
Also used : Locale(java.util.Locale) Account(com.zimbra.cs.account.Account) SMTPMessage(com.sun.mail.smtp.SMTPMessage) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MessagingException(javax.mail.MessagingException) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MailSender(com.zimbra.cs.mailbox.MailSender) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 100 with MessagingException

use of javax.mail.MessagingException in project zm-mailbox by Zimbra.

the class CalendarItem method createBlob.

/**
     * The Blob for the appointment/task is currently single Mime multipart/digest which has
     * each invite's MimeMessage stored as a part.
     *
     * @param invPm
     * @param firstInvite
     * @throws ServiceException
     */
private MailboxBlob createBlob(ParsedMessage invPm, Invite firstInvite) throws ServiceException {
    // Create blob only if there's an attachment or DESCRIPTION is too big to be stored in metadata.
    if (!firstInvite.hasAttachment() && (invPm == null || firstInvite.descInMeta())) {
        // If we're not going to create a blob after all, we must at least save the metadata to db.
        // It's weird that the db update is implicitly required of this method, but that's the way
        // it is, unfortunately.  If we're creating a blob, the implicit db update is done by
        // storeUpdatedBlob() call.  (see below)
        saveMetadata();
        return null;
    }
    try {
        // create the toplevel multipart/digest...
        MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
        MimeMultipart mmp = new ZMimeMultipart("digest");
        mm.setContent(mmp);
        // add the invite
        MimeBodyPart mbp = new ZMimeBodyPart();
        mbp.setDataHandler(new DataHandler(new PMDataSource(invPm)));
        mbp.addHeader("invId", Integer.toString(firstInvite.getMailItemId()));
        mmp.addBodyPart(mbp);
        mm.saveChanges();
        return storeUpdatedBlob(mm);
    } catch (MessagingException e) {
        throw ServiceException.FAILURE("MessagingException " + e, e);
    } catch (IOException e) {
        throw ServiceException.FAILURE("IOException " + e, e);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

MessagingException (javax.mail.MessagingException)324 MimeMessage (javax.mail.internet.MimeMessage)126 IOException (java.io.IOException)125 InternetAddress (javax.mail.internet.InternetAddress)64 MimeMultipart (javax.mail.internet.MimeMultipart)60 MimeBodyPart (javax.mail.internet.MimeBodyPart)58 Message (javax.mail.Message)45 Session (javax.mail.Session)43 InputStream (java.io.InputStream)41 Properties (java.util.Properties)38 Date (java.util.Date)35 ArrayList (java.util.ArrayList)33 PackageException (com.axway.ats.action.objects.model.PackageException)32 Address (javax.mail.Address)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)25 DataHandler (javax.activation.DataHandler)23 BodyPart (javax.mail.BodyPart)23