Search in sources :

Example 36 with Address

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

the class MailSender method updateHeaders.

void updateHeaders(MimeMessage mm, Account acct, Account authuser, OperationContext octxt, String originIP, boolean replyToSender) throws MessagingException, ServiceException {
    if (mSkipHeaderUpdate) {
        return;
    }
    Provisioning prov = Provisioning.getInstance();
    if (originIP != null) {
        boolean addOriginatingIP = prov.getConfig().isSmtpSendAddOriginatingIP();
        if (addOriginatingIP) {
            mm.addHeader(X_ORIGINATING_IP, formatXOrigIpHeader(originIP));
        }
    }
    boolean addMailer = prov.getConfig().isSmtpSendAddMailer();
    if (addMailer) {
        String ua = octxt != null ? octxt.getUserAgent() : null;
        String mailer = "Zimbra " + BuildInfo.VERSION + (ua == null ? "" : " (" + ua + ")");
        mm.addHeader(X_MAILER, mailer);
    }
    if (prov.getConfig().isSmtpSendAddAuthenticatedUser()) {
        mm.addHeader(X_AUTHENTICATED_USER, authuser.getName());
    }
    // set various headers on the outgoing message
    InternetAddress from = (InternetAddress) ArrayUtil.getFirstElement(mm.getFrom());
    InternetAddress sender = (InternetAddress) mm.getSender();
    if (mRedirectMode) {
    // Don't touch the message at all in redirect mode.
    } else {
        Pair<InternetAddress, InternetAddress> fromsender = getSenderHeaders(from, sender, acct, authuser, octxt != null ? octxt.isUsingAdminPrivileges() : false);
        from = fromsender.getFirst();
        sender = fromsender.getSecond();
    }
    mm.setFrom(from);
    mm.setSender(sender);
    mm.setSentDate(new Date());
    if (sender == null) {
        Address[] existingReplyTos = mm.getReplyTo();
        if (existingReplyTos == null || existingReplyTos.length == 0) {
            String replyTo = acct.getPrefReplyToAddress();
            if (replyTo != null && !replyTo.trim().isEmpty()) {
                mm.setHeader("Reply-To", replyTo);
            }
        }
    } else {
        if (replyToSender) {
            mm.setReplyTo(new Address[] { sender });
        }
    }
    updateReferenceHeaders(mm, octxt, authuser);
    mm.saveChanges();
}
Also used : 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) Provisioning(com.zimbra.cs.account.Provisioning) Date(java.util.Date)

Example 37 with Address

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

the class ForwardCalendarItem method setSentByAndAttendees.

private static void setSentByAndAttendees(ZVCalendar cal, String sentBy, Address[] rcpts) throws ServiceException {
    // Set SENT-BY to sender's email address in ORGANIZER property of all VEVENT/VTODO components.
    // Required by Outlook.
    // Also, remove existing ATTENDEEs and add ATTENDEE lines for forwardees.
    String sentByAddr = "mailto:" + sentBy;
    for (Iterator<ZComponent> compIter = cal.getComponentIterator(); compIter.hasNext(); ) {
        ZComponent comp = compIter.next();
        ICalTok compName = ICalTok.lookup(comp.getName());
        if (ICalTok.VEVENT.equals(compName) || ICalTok.VTODO.equals(compName)) {
            // Remove existing ATTENDEEs and X-MS-OLK-SENDER.
            for (Iterator<ZProperty> propIter = comp.getPropertyIterator(); propIter.hasNext(); ) {
                ZProperty prop = propIter.next();
                if (ICalTok.ATTENDEE.equals(prop.getToken()) || "X-MS-OLK-SENDER".equalsIgnoreCase(prop.getName()))
                    propIter.remove();
            }
            // SENT-BY
            ZProperty orgProp = comp.getProperty(ICalTok.ORGANIZER);
            if (orgProp != null) {
                ZParameter sentByParam = orgProp.getParameter(ICalTok.SENT_BY);
                if (sentByParam != null) {
                    sentByParam.setValue(sentByAddr);
                } else {
                    sentByParam = new ZParameter(ICalTok.SENT_BY, sentByAddr);
                    orgProp.addParameter(sentByParam);
                }
                // Set X-MS-OLK-SENDER, another Outlook special.
                ZProperty xMsOlkSender = new ZProperty("X-MS-OLK-SENDER");
                xMsOlkSender.setValue(sentByAddr);
                comp.addProperty(xMsOlkSender);
            }
            // ATTENDEEs
            if (rcpts == null)
                throw ServiceException.INVALID_REQUEST("Missing forwardees", null);
            for (Address r : rcpts) {
                InternetAddress rcpt = (InternetAddress) r;
                String email = "mailto:" + rcpt.getAddress();
                ZProperty att = new ZProperty(ICalTok.ATTENDEE, email);
                String name = rcpt.getPersonal();
                if (name != null && name.length() > 0)
                    att.addParameter(new ZParameter(ICalTok.CN, name));
                att.addParameter(new ZParameter(ICalTok.PARTSTAT, ICalTok.NEEDS_ACTION.toString()));
                att.addParameter(new ZParameter(ICalTok.RSVP, "TRUE"));
                comp.addProperty(att);
            }
        }
    }
}
Also used : ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) 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) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) ZParameter(com.zimbra.common.calendar.ZCalendar.ZParameter) ICalTok(com.zimbra.common.calendar.ZCalendar.ICalTok)

Example 38 with Address

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

the class ForwardCalendarItem method createMergedMessage.

// Take mmInv and mutate it.  text/calendar part is replaced by cal and plain and html parts
// are replaced by plainDescPart and htmlDescPart.
private static MimeMessage createMergedMessage(Account account, Address fromAddr, Address senderAddr, MimeMessage mmInv, Invite inv, ZVCalendar cal, MimeBodyPart plainDescPart, MimeBodyPart htmlDescPart) throws ServiceException {
    try {
        String uid = inv.getUid();
        if (mmInv != null) {
            // Get a copy so we can modify it.
            MimeMessage mm = new ZMimeMessage(mmInv);
            // Discard all old headers except Subject and Content-*.
            @SuppressWarnings("rawtypes") Enumeration eh = mmInv.getAllHeaders();
            while (eh.hasMoreElements()) {
                Header hdr = (Header) eh.nextElement();
                String hdrNameUpper = hdr.getName().toUpperCase();
                if (!hdrNameUpper.startsWith("CONTENT-") && !hdrNameUpper.equals("SUBJECT")) {
                    mm.removeHeader(hdr.getName());
                }
            }
            mm.setSentDate(new Date());
            mm.setRecipients(javax.mail.Message.RecipientType.TO, (Address[]) null);
            mm.setRecipients(javax.mail.Message.RecipientType.CC, (Address[]) null);
            mm.setRecipients(javax.mail.Message.RecipientType.BCC, (Address[]) null);
            if (fromAddr != null)
                mm.setFrom(fromAddr);
            if (senderAddr != null)
                mm.setSender(senderAddr);
            // Find and replace the existing calendar part with the new calendar object.
            ReplacingVisitor visitor = new ReplacingVisitor(uid, cal, plainDescPart, htmlDescPart);
            visitor.accept(mm);
            mm.saveChanges();
            return mm;
        } else {
            String subject = inv.getName();
            String desc, descHtml;
            try {
                Object plainContent = plainDescPart != null ? plainDescPart.getContent() : null;
                desc = plainContent != null ? plainContent.toString() : null;
                Object htmlContent = htmlDescPart != null ? htmlDescPart.getContent() : null;
                descHtml = htmlContent != null ? htmlContent.toString() : null;
            } catch (MessagingException e) {
                throw ServiceException.FAILURE("Messaging Exception while retrieving description", e);
            } catch (IOException e) {
                throw ServiceException.FAILURE("Messaging Exception while retrieving description", e);
            }
            return CalendarMailSender.createCalendarMessage(account, fromAddr, senderAddr, null, subject, desc, descHtml, uid, cal, false);
        }
    } catch (MessagingException e) {
        throw ServiceException.FAILURE("Messaging Exception while building calendar message from source MimeMessage", e);
    }
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Enumeration(java.util.Enumeration) Header(javax.mail.Header) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Date(java.util.Date)

Example 39 with Address

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

the class ParsedMessage method getSenderEmail.

/**
     * If fromFirst is true, returns the email address of the first <tt>From</tt>
     * or <tt>Sender</tt> header.  If fromFirst is false, returns the email address
     * of the first <tt>Sender</tt> or <tt>From</tt> header.
     */
public String getSenderEmail(boolean fromFirst) {
    try {
        if (fromFirst) {
            // From header first, then Sender
            Address[] froms = getMimeMessage().getFrom();
            if (froms != null && froms.length > 0 && froms[0] instanceof InternetAddress) {
                return ((InternetAddress) froms[0]).getAddress();
            }
            Address sender = getMimeMessage().getSender();
            if (sender instanceof InternetAddress) {
                return ((InternetAddress) sender).getAddress();
            }
        } else {
            // Sender header first, then From
            Address sender = getMimeMessage().getSender();
            if (sender instanceof InternetAddress) {
                return ((InternetAddress) sender).getAddress();
            }
            Address[] froms = getMimeMessage().getFrom();
            if (froms != null && froms.length > 0 && froms[0] instanceof InternetAddress) {
                return ((InternetAddress) froms[0]).getAddress();
            }
        }
    } catch (MessagingException e) {
    }
    return null;
}
Also used : 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) MessagingException(javax.mail.MessagingException)

Example 40 with Address

use of javax.mail.Address 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)

Aggregations

Address (javax.mail.Address)89 InternetAddress (javax.mail.internet.InternetAddress)69 MessagingException (javax.mail.MessagingException)35 MimeMessage (javax.mail.internet.MimeMessage)34 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)21 ArrayList (java.util.ArrayList)20 Date (java.util.Date)19 MimeBodyPart (javax.mail.internet.MimeBodyPart)13 Account (com.zimbra.cs.account.Account)12 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)10 IOException (java.io.IOException)9 Locale (java.util.Locale)9 Message (javax.mail.Message)9 ItemId (com.zimbra.cs.service.util.ItemId)8 Header (javax.mail.Header)8 NHINDAddress (org.nhindirect.stagent.NHINDAddress)8 AddressException (javax.mail.internet.AddressException)7 MimeMultipart (javax.mail.internet.MimeMultipart)7 ZVCalendar (com.zimbra.common.calendar.ZCalendar.ZVCalendar)6 Invite (com.zimbra.cs.mailbox.calendar.Invite)6