Search in sources :

Example 31 with Address

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

the class CalendarMailSender method createCalendarMessage.

public static MimeMessage createCalendarMessage(Account account, Address fromAddr, Address senderAddr, List<Address> toAddrs, MimeMessage srcMm, Invite inv, ZVCalendar cal, boolean replyToSender) throws ServiceException {
    try {
        String uid = inv.getUid();
        if (srcMm != null) {
            // Get a copy so we can modify it.
            MimeMessage mm = new ZMimeMessage(srcMm);
            // Discard all old headers except Subject and Content-*.
            Enumeration eh = srcMm.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());
            if (toAddrs != null) {
                Address[] addrs = new Address[toAddrs.size()];
                toAddrs.toArray(addrs);
                mm.setRecipients(javax.mail.Message.RecipientType.TO, addrs);
            } else {
                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);
                if (replyToSender)
                    mm.setReplyTo(new Address[] { senderAddr });
            }
            // Find and replace the existing calendar part with the new calendar object.
            CalendarPartReplacingVisitor visitor = new CalendarPartReplacingVisitor(uid, cal);
            visitor.accept(mm);
            mm.saveChanges();
            return mm;
        } else {
            String subject = inv.getName();
            String desc = inv.getDescription();
            String descHtml = inv.getDescriptionHtml();
            return createCalendarMessage(account, fromAddr, senderAddr, toAddrs, 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) Date(java.util.Date)

Example 32 with Address

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

the class CalendarMailSender method createOrganizerChangeMessage.

public static MimeMessage createOrganizerChangeMessage(Account fromAccount, Account authAccount, boolean asAdmin, CalendarItem calItem, Invite inv, List<Address> rcpts) throws ServiceException {
    ZOrganizer organizer = inv.getOrganizer();
    assert (organizer != null);
    boolean onBehalfOf = organizer.hasSentBy();
    String senderAddr = onBehalfOf ? organizer.getSentBy() : organizer.getAddress();
    Locale locale = fromAccount.getLocale();
    boolean hidePrivate = !calItem.isPublic() && !calItem.allowPrivateAccess(authAccount, asAdmin);
    String subject;
    if (hidePrivate) {
        subject = L10nUtil.getMessage(MsgKey.calendarSubjectWithheld, locale);
    } else {
        subject = inv.getName();
    }
    StringBuilder sb = new StringBuilder("Organizer has been changed to " + fromAccount.getName());
    sb.append("\r\n\r\n");
    if (!hidePrivate) {
        MimeMessage mmInv = inv.getMimeMessage();
        if (mmInv != null) {
            attachInviteSummary(sb, inv, mmInv, locale);
        }
    }
    ZVCalendar iCal = inv.newToICalendar(true);
    Address from = AccountUtil.getFriendlyEmailAddress(fromAccount);
    Address sender = null;
    if (onBehalfOf) {
        try {
            sender = new JavaMailInternetAddress(senderAddr);
        } catch (AddressException e) {
            throw MailServiceException.ADDRESS_PARSE_ERROR(e);
        }
    }
    return createCalendarMessage(authAccount, from, sender, rcpts, subject, sb.toString(), null, inv.getUid(), iCal);
}
Also used : Locale(java.util.Locale) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) 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) AddressException(javax.mail.internet.AddressException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress)

Example 33 with Address

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

the class CalendarMailSender method createForwardedInviteMessage.

public static MimeMessage createForwardedInviteMessage(MimeMessage mmOrig, String origSenderEmail, String forwarderEmail, String[] forwardTo) {
    List<Address> rcpts = new ArrayList<Address>();
    for (String to : forwardTo) {
        try {
            rcpts.add(new JavaMailInternetAddress(to));
        } catch (AddressException e) {
            ZimbraLog.calendar.warn("Ignoring invalid address \"" + to + "\" during invite forward");
        }
    }
    if (rcpts.isEmpty())
        return null;
    MimeMessage mm = null;
    try {
        mm = new ZMimeMessage(mmOrig);
        mm.removeHeader("To");
        mm.removeHeader("Cc");
        mm.removeHeader("Bcc");
        mm.addRecipients(RecipientType.TO, rcpts.toArray(new Address[0]));
        // Set Reply-To to the original sender.
        mm.setReplyTo(new Address[] { new JavaMailInternetAddress(origSenderEmail) });
        mm.removeHeader("Date");
        mm.removeHeader("Message-ID");
        mm.removeHeader("Return-Path");
        mm.removeHeader("Received");
        // Set special header to indicate the forwarding attendee.
        mm.setHeader(CalendarMailSender.X_ZIMBRA_CALENDAR_INTENDED_FOR, forwarderEmail);
        mm.saveChanges();
    } catch (MessagingException e) {
        ZimbraLog.calendar.warn("Unable to compose email for invite forwarding", e);
    }
    return mm;
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) 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) AddressException(javax.mail.internet.AddressException) ArrayList(java.util.ArrayList) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress)

Example 34 with Address

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

the class CalendarMailSender method createForwardedPrivateInviteMessage.

public static MimeMessage createForwardedPrivateInviteMessage(Account account, Locale lc, String method, List<Invite> invites, String origSenderEmail, String forwarderEmail, String[] forwardTo) throws ServiceException {
    if (invites == null || invites.isEmpty())
        return null;
    List<Address> rcpts = new ArrayList<Address>();
    for (String to : forwardTo) {
        try {
            rcpts.add(new JavaMailInternetAddress(to));
        } catch (AddressException e) {
            ZimbraLog.calendar.warn("Ignoring invalid address \"" + to + "\" during invite forward");
        }
    }
    if (rcpts.isEmpty())
        return null;
    String subject = L10nUtil.getMessage(MsgKey.calendarSubjectWithheld, lc);
    // Create filtered version of invites.
    List<Invite> filteredInvs = new ArrayList<Invite>();
    for (Invite inv : invites) {
        Invite filtered = inv.newCopy();
        filtered.clearAlarms();
        filtered.clearPrivateInfo();
        filtered.setName(subject);
        // Add ATTENDEE for forwarder.
        List<ZAttendee> atts = inv.getAttendees();
        if (atts != null && forwarderEmail != null) {
            for (ZAttendee att : atts) {
                if (forwarderEmail.equalsIgnoreCase(att.getAddress())) {
                    filtered.addAttendee(att);
                }
            }
        }
        filteredInvs.add(filtered);
    }
    MimeMessage mm = null;
    try {
        mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(account));
        mm.setFrom(new JavaMailInternetAddress(origSenderEmail));
        mm.addRecipients(RecipientType.TO, rcpts.toArray(new Address[0]));
        // Set special header to indicate the forwarding attendee.
        mm.setHeader(CalendarMailSender.X_ZIMBRA_CALENDAR_INTENDED_FOR, forwarderEmail);
        mm.setSubject(subject);
        StringWriter writer = new StringWriter();
        try {
            writer.write("BEGIN:VCALENDAR\r\n");
            ZProperty prop;
            prop = new ZProperty(ICalTok.PRODID, ZCalendar.sZimbraProdID);
            prop.toICalendar(writer);
            prop = new ZProperty(ICalTok.VERSION, ZCalendar.sIcalVersion);
            prop.toICalendar(writer);
            prop = new ZProperty(ICalTok.METHOD, method);
            prop.toICalendar(writer);
            // timezones
            Invite firstInv = filteredInvs.get(0);
            TimeZoneMap tzmap = new TimeZoneMap(firstInv.getTimeZoneMap().getLocalTimeZone());
            for (Invite inv : filteredInvs) {
                tzmap.add(inv.getTimeZoneMap());
            }
            for (Iterator<ICalTimeZone> iter = tzmap.tzIterator(); iter.hasNext(); ) {
                ICalTimeZone tz = iter.next();
                tz.newToVTimeZone().toICalendar(writer);
            }
            // VEVENTs/VTODOs
            for (Invite inv : filteredInvs) {
                ZComponent comp = inv.newToVComponent(false, true);
                comp.toICalendar(writer);
            }
            writer.write("END:VCALENDAR\r\n");
        } catch (IOException e) {
            throw ServiceException.FAILURE("Error writing iCalendar", e);
        } finally {
            Closeables.closeQuietly(writer);
        }
        mm.setText(writer.toString());
        ContentType ct = new ContentType(MimeConstants.CT_TEXT_CALENDAR);
        ct.setParameter(MimeConstants.P_CHARSET, MimeConstants.P_CHARSET_UTF8);
        ct.setParameter("method", method);
        mm.setHeader("Content-Type", ct.toString());
    } catch (MessagingException e) {
        ZimbraLog.calendar.warn("Unable to compose email for invite forwarding", e);
    }
    return mm;
}
Also used : Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Mime(com.zimbra.cs.mime.Mime) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) StringWriter(java.io.StringWriter) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) TimeZoneMap(com.zimbra.common.calendar.TimeZoneMap) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone)

Example 35 with Address

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

the class DefaultTnefToICalendar method addAttendees.

/**
     * 
     * @param icalOutput
     * @param mimeMsg
     * @param partstat
     * @param replyWanted
     * @throws ParserException
     * @throws URISyntaxException
     * @throws IOException
     * @throws ParseException
     * @throws MessagingException
     */
private void addAttendees(ContentHandler icalOutput, MimeMessage mimeMsg, PartStat partstat, boolean replyWanted) throws ParserException, URISyntaxException, IOException, ParseException, MessagingException {
    // ATTENDEEs
    InternetAddress firstFromIA = null;
    String firstFromEmailAddr = null;
    // Use for SENT-BY if applicable
    String senderMailto = null;
    String senderCn = null;
    javax.mail.Address[] toRecips = null;
    javax.mail.Address[] ccRecips = null;
    javax.mail.Address[] bccRecips = null;
    javax.mail.Address[] msgFroms = null;
    javax.mail.Address msgSender = null;
    if (mimeMsg != null) {
        toRecips = mimeMsg.getRecipients(javax.mail.Message.RecipientType.TO);
        ccRecips = mimeMsg.getRecipients(javax.mail.Message.RecipientType.CC);
        bccRecips = mimeMsg.getRecipients(javax.mail.Message.RecipientType.BCC);
        msgFroms = mimeMsg.getFrom();
        msgSender = mimeMsg.getSender();
    }
    if (msgFroms != null) {
        if (msgFroms.length != 1) {
            sLog.debug(msgFroms.length + " From: recipients for " + method.getValue());
        }
        if (msgFroms.length >= 1) {
            firstFromIA = (InternetAddress) msgFroms[0];
            firstFromEmailAddr = firstFromIA.getAddress();
        }
        if (msgSender != null) {
            String senderAddr = msgSender.toString();
            if (msgSender instanceof InternetAddress) {
                InternetAddress senderIA = (InternetAddress) msgSender;
                senderAddr = senderIA.getAddress();
                senderCn = senderIA.getPersonal();
                if (!firstFromIA.equals(senderIA)) {
                    senderMailto = "Mailto:" + senderAddr;
                }
            }
        }
    }
    if (method.equals(Method.REPLY) || method.equals(Method.COUNTER)) {
        // from ATTENDEE to ORGANIZER
        if (toRecips != null) {
            if (toRecips.length != 1) {
                sLog.debug(toRecips.length + " To: recipients for " + method.getValue());
            }
            if (toRecips.length >= 1) {
                InternetAddress ia = (InternetAddress) toRecips[0];
                String email = ia.getAddress();
                String displayName = ia.getPersonal();
                icalOutput.startProperty(Property.ORGANIZER);
                icalOutput.propertyValue("Mailto:" + email);
                if (displayName != null) {
                    icalOutput.parameter(Parameter.CN, displayName);
                }
                icalOutput.endProperty(Property.ORGANIZER);
            }
        }
        if (firstFromEmailAddr != null) {
            String displayName = firstFromIA.getPersonal();
            icalOutput.startProperty(Property.ATTENDEE);
            icalOutput.propertyValue("Mailto:" + firstFromEmailAddr);
            if (displayName != null) {
                icalOutput.parameter(Parameter.CN, displayName);
            }
            icalOutput.parameter(Parameter.CUTYPE, CuType.INDIVIDUAL.getValue());
            if (partstat != null) {
                icalOutput.parameter(Parameter.PARTSTAT, partstat.getValue());
            }
            if (senderMailto != null) {
                icalOutput.parameter(Parameter.SENT_BY, senderMailto);
            }
            icalOutput.endProperty(Property.ATTENDEE);
        }
    } else {
        // ORGANIZER to ATTENDEEs - REQUEST or CANCEL
        InternetAddress organizerEmail = null;
        if (firstFromEmailAddr != null) {
            SentBy sentBy = null;
            Cn cn = null;
            if (senderMailto != null) {
                sentBy = new SentBy(senderMailto);
            }
            organizerEmail = firstFromIA;
            String displayName = firstFromIA.getPersonal();
            if ((displayName != null) && (!displayName.equals(firstFromEmailAddr))) {
                cn = new Cn(displayName);
            }
            Organizer organizer = new Organizer();
            organizer.setValue("Mailto:" + firstFromEmailAddr);
            if (cn != null) {
                organizer.getParameters().add(cn);
            }
            if (sentBy != null) {
                organizer.getParameters().add(sentBy);
            }
            IcalUtil.addProperty(icalOutput, organizer);
            if (icalType == ICALENDAR_TYPE.VEVENT) {
                // Assumption - ORGANIZER is an attendee and is attending.
                Attendee attendee = new Attendee("Mailto:" + firstFromEmailAddr);
                if (cn != null) {
                    attendee.getParameters().add(cn);
                }
                attendee.getParameters().add(CuType.INDIVIDUAL);
                attendee.getParameters().add(Role.REQ_PARTICIPANT);
                if (!method.equals(Method.CANCEL)) {
                    PartStat orgPartstat = PartStat.ACCEPTED;
                    if (ccRecips != null) {
                        for (Address a : ccRecips) {
                            InternetAddress ia = (InternetAddress) a;
                            if (organizerEmail.equals(ia)) {
                                orgPartstat = PartStat.TENTATIVE;
                                break;
                            }
                        }
                    }
                    attendee.getParameters().add(orgPartstat);
                }
                // Was including SENT-BY but probably not appropriate
                // for a request
                IcalUtil.addProperty(icalOutput, attendee);
            }
        }
        if (toRecips != null) {
            for (Address a : toRecips) {
                InternetAddress ia = (InternetAddress) a;
                if ((organizerEmail != null) && organizerEmail.equals(ia)) {
                    // No need to add the information twice
                    continue;
                }
                addAttendee(icalOutput, ia, Role.REQ_PARTICIPANT, CuType.INDIVIDUAL, partstat, replyWanted);
            }
        }
        if (ccRecips != null) {
            for (Address a : ccRecips) {
                InternetAddress ia = (InternetAddress) a;
                if ((organizerEmail != null) && organizerEmail.equals(ia)) {
                    // No need to add the information twice
                    continue;
                }
                addAttendee(icalOutput, ia, Role.OPT_PARTICIPANT, CuType.INDIVIDUAL, partstat, replyWanted);
            }
        }
        if (bccRecips != null) {
            for (Address a : bccRecips) {
                InternetAddress ia = (InternetAddress) a;
                addAttendee(icalOutput, ia, Role.NON_PARTICIPANT, CuType.RESOURCE, partstat, replyWanted);
            }
        }
    }
    if (senderMailto != null) {
        XProperty msOlkSender = new XProperty("X-MS-OLK-SENDER", senderMailto);
        if (senderCn != null) {
            Cn cn = new Cn(senderCn);
            msOlkSender.getParameters().add(cn);
        }
        IcalUtil.addProperty(icalOutput, msOlkSender);
    }
}
Also used : Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) XProperty(net.fortuna.ical4j.model.property.XProperty) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) Organizer(net.fortuna.ical4j.model.property.Organizer) SentBy(net.fortuna.ical4j.model.parameter.SentBy) PartStat(net.fortuna.ical4j.model.parameter.PartStat) Cn(net.fortuna.ical4j.model.parameter.Cn) Attendee(net.fortuna.ical4j.model.property.Attendee)

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