Search in sources :

Example 16 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.

the class CalendarMailSender method createForwardNotifyMessage.

public static MimeMessage createForwardNotifyMessage(Account senderAcct, Account toAcct, String to, Address[] rcpts, Invite inv) throws MessagingException, ServiceException {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
    Locale lc = toAcct.getLocale();
    mm.setSubject(L10nUtil.getMessage(MsgKey.calendarForwardNotificationSubject, lc, inv.getName()), MimeConstants.P_CHARSET_UTF8);
    mm.setSentDate(new Date());
    String postmaster = senderAcct.getAttr(Provisioning.A_zimbraNewMailNotificationFrom);
    Map<String, String> vars = new HashMap<String, String>();
    vars.put("RECIPIENT_DOMAIN", senderAcct.getDomainName());
    postmaster = StringUtil.fillTemplate(postmaster, vars);
    mm.setSender(new JavaMailInternetAddress(postmaster));
    mm.setFrom(new JavaMailInternetAddress(senderAcct.getName()));
    mm.setRecipient(RecipientType.TO, new JavaMailInternetAddress(to));
    MimeMultipart mmp = new ZMimeMultipart("alternative");
    mm.setContent(mmp);
    String sender = senderAcct.getCn() + " <" + senderAcct.getName() + ">";
    String time = FriendlyCalendaringDescription.getTimeDisplayString(inv.getStartTime(), inv.getEndTime(), inv.isRecurrence(), inv.isAllDayEvent(), lc, toAcct);
    StringBuilder sb = new StringBuilder();
    StringBuilder sbHtml = new StringBuilder();
    for (Address rcpt : rcpts) {
        sb.append(rcpt.toString()).append("\n\t");
        InternetAddress address = new JavaMailInternetAddress(rcpt.toString());
        sbHtml.append("<a href=\"mailto:").append(address.getAddress()).append("\">");
        if (address.getPersonal() != null) {
            sbHtml.append(address.getPersonal()).append("</a>").append("<br>");
        } else {
            sbHtml.append(address.getAddress()).append("</a>").append("<br>");
        }
    }
    String recipients = sb.toString();
    String recipientsHtml = sbHtml.toString();
    if (inv.isRecurrence()) {
        ZRecur zr = FriendlyCalendaringDescription.getRecur(inv);
        time += " (" + FriendlyCalendaringDescription.getRecurrenceDisplayString(zr, inv.getStartTime().getCalendarCopy(), lc) + ")";
    }
    String text = L10nUtil.getMessage(MsgKey.calendarForwardNotificationBody, lc, sender, inv.getName(), time, recipients);
    MimeBodyPart textPart = new ZMimeBodyPart();
    textPart.setText(text, MimeConstants.P_CHARSET_UTF8);
    mmp.addBodyPart(textPart);
    sender = "<a href=\"mailto:" + senderAcct.getName() + "\">" + senderAcct.getCn() + "</a>";
    String html = L10nUtil.getMessage(MsgKey.calendarForwardNotificationBodyHtml, lc, sender, inv.getName(), time, recipientsHtml);
    MimeBodyPart htmlPart = new ZMimeBodyPart();
    htmlPart.setContent(html, MimeConstants.CT_TEXT_HTML + "; " + MimeConstants.P_CHARSET + "=" + MimeConstants.P_CHARSET_UTF8);
    mmp.addBodyPart(htmlPart);
    mm.saveChanges();
    return mm;
}
Also used : Locale(java.util.Locale) 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) HashMap(java.util.HashMap) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) Date(java.util.Date) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) 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 17 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress 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 18 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress 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 {
            IOUtils.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 19 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.

the class CalItemSmsReminderTask method sendReminder.

@Override
protected void sendReminder(CalendarItem calItem, Invite invite) throws Exception {
    Account account = calItem.getAccount();
    Locale locale = account.getLocale();
    TimeZone tz = Util.getAccountTimeZone(account);
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(account));
    String to = account.getAttr(Provisioning.A_zimbraCalendarReminderDeviceEmail);
    if (to == null) {
        ZimbraLog.scheduler.info("Unable to send calendar reminder sms since %s is not set", Provisioning.A_zimbraCalendarReminderDeviceEmail);
        return;
    }
    mm.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(to));
    mm.setText(getText(calItem, invite, locale, tz), MimeConstants.P_CHARSET_UTF8);
    mm.saveChanges();
    MailSender mailSender = calItem.getMailbox().getMailSender();
    mailSender.setSaveToSent(false);
    mailSender.sendMimeMessage(null, calItem.getMailbox(), mm);
}
Also used : Locale(java.util.Locale) Account(com.zimbra.cs.account.Account) TimeZone(java.util.TimeZone) MimeMessage(javax.mail.internet.MimeMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MailSender(com.zimbra.cs.mailbox.MailSender)

Example 20 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.

the class FeedManager method parseAtomFeed.

private static SubscriptionData<ParsedMessage> parseAtomFeed(Element feed, Folder.SyncData fsd, long lastModified) throws ServiceException {
    try {
        // get defaults from the <feed> element
        InternetAddress addrFeed = parseAtomAuthor(feed.getOptionalElement("author"), null);
        if (addrFeed == null) {
            addrFeed = new JavaMailInternetAddress("", stripXML(feed.getAttribute("title")), "utf-8");
        }
        Date dateFeed = DateUtil.parseISO8601Date(feed.getAttribute("updated", null), new Date());
        List<Enclosure> enclosures = new ArrayList<Enclosure>();
        SubscriptionData<ParsedMessage> sdata = new SubscriptionData<ParsedMessage>();
        for (Element item : feed.listElements("entry")) {
            // get the item's date
            Date date = DateUtil.parseISO8601Date(item.getAttribute("updated", null), null);
            if (date == null) {
                date = DateUtil.parseISO8601Date(item.getAttribute("modified", null), dateFeed);
            }
            // construct an address for the author
            InternetAddress addr = parseAtomAuthor(item.getOptionalElement("author"), addrFeed);
            // get the item's title (may be html or xhtml)
            Element tblock = item.getElement("title");
            String type = tblock.getAttribute("type", "text").trim().toLowerCase();
            String title = tblock.getText();
            if (type.equals("html") || type.equals("xhtml") || type.equals("text/html") || type.equals("application/xhtml+xml")) {
                title = stripXML(title);
            }
            // find the item's link and any enclosures (associated media links)
            enclosures.clear();
            String href = "";
            for (Element link : item.listElements("link")) {
                String relation = link.getAttribute("rel", "alternate");
                if (relation.equals("alternate")) {
                    href = link.getAttribute("href");
                } else if (relation.equals("enclosure")) {
                    enclosures.add(new Enclosure(link.getAttribute("href", null), link.getAttribute("title", null), link.getAttribute("type", null)));
                }
            }
            String guid = item.getAttribute("id", href);
            // make sure we haven't already seen this item
            if (fsd != null && fsd.alreadySeen(guid == null || guid.equals("") ? null : guid, date == dateFeed ? null : date))
                continue;
            // get the content/summary and markup
            Element content = item.getOptionalElement("content");
            if (content == null) {
                content = item.getOptionalElement("summary");
            }
            if (content == null)
                continue;
            type = content.getAttribute("type", "text").trim().toLowerCase();
            boolean html = false;
            if (type.equals("html") || type.equals("xhtml") || type.equals("text/html") || type.equals("application/xhtml+xml")) {
                html = true;
            } else if (!type.equals("text") && !type.equals("text/plain")) {
                throw ServiceException.PARSE_ERROR("unsupported atom entry content type: " + type, null);
            }
            String text = content.getText();
            if (Strings.isNullOrEmpty(text)) {
                Element div = content.getElement("div");
                if (div != null) {
                    /*
                         * Assume it is this variant:
                         * http://tools.ietf.org/html/rfc4287#section-4.1.3
                         *   atomInlineXHTMLContent = element atom:content { atomCommonAttributes,
                         *           attribute type { "xhtml" }, xhtmlDiv }
                         */
                    text = div.getText();
                }
            }
            ParsedMessage pm = generateMessage(title, text, href, html, addr, date, enclosures);
            sdata.recordItem(pm, guid, date.getTime());
        }
        sdata.recordFeedModifiedDate(lastModified);
        return sdata;
    } catch (UnsupportedEncodingException e) {
        throw ServiceException.FAILURE("error encoding atom feed name", e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Element(com.zimbra.common.soap.Element) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Date(java.util.Date) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress)

Aggregations

JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)46 InternetAddress (javax.mail.internet.InternetAddress)31 MimeMessage (javax.mail.internet.MimeMessage)25 AddressException (javax.mail.internet.AddressException)17 Date (java.util.Date)15 MessagingException (javax.mail.MessagingException)14 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)13 Account (com.zimbra.cs.account.Account)12 MailSender (com.zimbra.cs.mailbox.MailSender)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9 ArrayList (java.util.ArrayList)9 Address (javax.mail.Address)9 ServiceException (com.zimbra.common.service.ServiceException)8 Locale (java.util.Locale)8 SMTPMessage (com.sun.mail.smtp.SMTPMessage)7 MimeBodyPart (javax.mail.internet.MimeBodyPart)7 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)6 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)6 IOException (java.io.IOException)6 MimeMultipart (javax.mail.internet.MimeMultipart)6