Search in sources :

Example 36 with JavaMailInternetAddress

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

the class CalendarRequestTest method testNotifyCalendarItem.

/**
 * Test method for {@link com.zimbra.cs.service.mail.CalendarRequest#notifyCalendarItem(com.zimbra.soap.ZimbraSoapContext, com.zimbra.cs.mailbox.OperationContext, com.zimbra.cs.account.Account, com.zimbra.cs.mailbox.Mailbox, com.zimbra.cs.mailbox.CalendarItem, boolean, java.util.List, boolean, com.zimbra.cs.service.mail.CalendarRequest.MailSendQueue)}.
 * @throws Exception
 */
@Test
public void testNotifyCalendarItem() throws Exception {
    JavaMailInternetAddress emailAddress = new JavaMailInternetAddress("test@zimbra.com", "test", MimeConstants.P_CHARSET_UTF8);
    PowerMockito.mockStatic(AccountUtil.class);
    PowerMockito.doReturn(emailAddress).when(AccountUtil.class, "getFriendlyEmailAddress", account);
    List<Address> addressList = new ArrayList<Address>();
    addressList.add((Address) new InternetAddress("test1@zimbra.com", "Test 1"));
    addressList.add((Address) new InternetAddress("test2@zimbra.com", "Test 2"));
    List<ZAttendee> attendeeList = new ArrayList<ZAttendee>();
    attendeeList.add(addedAttendee1);
    attendeeList.add(addedAttendee2);
    PowerMockito.doReturn(System.currentTimeMillis()).when(octxt, "getTimestamp");
    PowerMockito.doReturn(1).when(invite1).getMailItemId();
    PowerMockito.doReturn(2).when(invite2).getMailItemId();
    PowerMockito.doReturn(attendeeList).when(invite1).getAttendees();
    PowerMockito.doReturn(attendeeList).when(invite2).getAttendees();
    PowerMockito.doReturn(true).when(invite2).hasRecurId();
    PowerMockito.when(calItem.isPublic()).thenReturn(true);
    PowerMockito.when(calItem.allowPrivateAccess(account, true)).thenReturn(true);
    PowerMockito.when(calItem.getId()).thenReturn(1);
    PowerMockito.when(calItem.getInvites()).thenReturn(new Invite[] { invite1, invite2 });
    PowerMockito.when(calItem.getSubpartMessage(1)).thenReturn(mm);
    PowerMockito.when(calItem.getSubpartMessage(2)).thenReturn(mm2);
    PowerMockito.when(mbox.getCalendarItemById(octxt, calItem.getId())).thenReturn(calItem);
    PowerMockito.mockStatic(CalendarMailSender.class);
    PowerMockito.doReturn(addressList).when(CalendarMailSender.class, "toListFromAttendees", attendeeList);
    PowerMockito.stub(PowerMockito.method(CalendarRequest.class, "isOnBehalfOfRequest", ZimbraSoapContext.class)).toReturn(false);
    PowerMockito.stub(PowerMockito.method(CalendarRequest.class, "getAuthenticatedAccount", ZimbraSoapContext.class)).toReturn(account);
    CalendarRequest.notifyCalendarItem(zsc, octxt, account, mbox, calItem, true, attendeeList, true, sendQueue);
    assertEquals(1, sendQueue.queue.size());
}
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) ZAttendee(com.zimbra.cs.mailbox.calendar.ZAttendee) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ArrayList(java.util.ArrayList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 37 with JavaMailInternetAddress

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

the class CalendarMailSender method sendInviteDeniedMessage.

public static void sendInviteDeniedMessage(final OperationContext octxt, Account fromAccount, Account senderAccount, boolean onBehalfOf, boolean allowPrivateAccess, final Mailbox mbox, final ItemId origMsgId, String toEmail, Invite inv) throws ServiceException {
    Address toAddr;
    try {
        toAddr = new JavaMailInternetAddress(toEmail);
    } catch (AddressException e) {
        throw ServiceException.FAILURE("Bad address: " + toEmail, e);
    }
    MsgKey bodyTextKey;
    if (fromAccount instanceof CalendarResource)
        bodyTextKey = MsgKey.calendarResourceDefaultReplyPermissionDenied;
    else
        bodyTextKey = MsgKey.calendarUserReplyPermissionDenied;
    final MimeMessage mm = createCalendarInviteDeniedMessage(fromAccount, senderAccount, onBehalfOf, allowPrivateAccess, toAddr, inv, bodyTextKey);
    // Send in a separate thread to avoid nested transaction error when saving a copy to Sent folder.
    Runnable r = new Runnable() {

        @Override
        public void run() {
            try {
                MailSender mailSender = getCalendarMailSender(mbox).setSendPartial(true);
                mailSender.sendMimeMessage(octxt, mbox, true, mm, null, origMsgId, MailSender.MSGTYPE_REPLY, null, false);
            } catch (ServiceException e) {
                ZimbraLog.calendar.warn("Ignoring error while sending permission-denied auto reply", e);
            } catch (OutOfMemoryError e) {
                Zimbra.halt("OutOfMemoryError while sending permission-denied auto reply", e);
            }
        }
    };
    Thread senderThread = new Thread(r, "CalendarPermDeniedReplySender");
    senderThread.setDaemon(true);
    senderThread.start();
}
Also used : Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) MsgKey(com.zimbra.common.util.L10nUtil.MsgKey) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MailSender(com.zimbra.cs.mailbox.MailSender) CalendarResource(com.zimbra.cs.account.CalendarResource)

Example 38 with JavaMailInternetAddress

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

the class CalItemEmailReminderTask 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_zimbraPrefCalendarReminderEmail);
    if (to == null) {
        ZimbraLog.scheduler.info("Unable to send calendar reminder email since %s is not set", Provisioning.A_zimbraPrefCalendarReminderEmail);
        return;
    }
    mm.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(to));
    mm.setSubject(L10nUtil.getMessage(calItem.getType() == MailItem.Type.APPOINTMENT ? L10nUtil.MsgKey.apptReminderEmailSubject : L10nUtil.MsgKey.taskReminderEmailSubject, locale, calItem.getSubject()), MimeConstants.P_CHARSET_UTF8);
    if (invite.getDescriptionHtml() == null) {
        mm.setText(getBody(calItem, invite, false, locale, tz), MimeConstants.P_CHARSET_UTF8);
    } else {
        MimeMultipart mmp = new ZMimeMultipart("alternative");
        mm.setContent(mmp);
        MimeBodyPart textPart = new ZMimeBodyPart();
        textPart.setText(getBody(calItem, invite, false, locale, tz), MimeConstants.P_CHARSET_UTF8);
        mmp.addBodyPart(textPart);
        MimeBodyPart htmlPart = new ZMimeBodyPart();
        htmlPart.setContent(getBody(calItem, invite, true, locale, tz), MimeConstants.CT_TEXT_HTML + "; " + MimeConstants.P_CHARSET + "=" + MimeConstants.P_CHARSET_UTF8);
        mmp.addBodyPart(htmlPart);
    }
    mm.setSentDate(new Date());
    mm.saveChanges();
    MailSender mailSender = calItem.getMailbox().getMailSender();
    mailSender.sendMimeMessage(null, calItem.getMailbox(), mm);
}
Also used : Locale(java.util.Locale) Account(com.zimbra.cs.account.Account) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MailSender(com.zimbra.cs.mailbox.MailSender) Date(java.util.Date) TimeZone(java.util.TimeZone) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) 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 39 with JavaMailInternetAddress

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

the class FeedManager method parseRssFeed.

private static SubscriptionData<ParsedMessage> parseRssFeed(Element root, Folder.SyncData fsd, long lastModified) throws ServiceException {
    try {
        String rname = root.getName();
        if (rname.equals("feed")) {
            return parseAtomFeed(root, fsd, lastModified);
        }
        Element channel = root.getElement("channel");
        String hrefChannel = channel.getAttribute("link");
        String subjChannel = channel.getAttribute("title");
        InternetAddress addrChannel = new JavaMailInternetAddress("", subjChannel, "utf-8");
        Date dateChannel = DateUtil.parseRFC2822Date(channel.getAttribute("lastBuildDate", null), new Date());
        List<Enclosure> enclosures = new ArrayList<Enclosure>(3);
        SubscriptionData<ParsedMessage> sdata = new SubscriptionData<ParsedMessage>();
        if (rname.equals("rss")) {
            root = channel;
        } else if (!rname.equals("RDF")) {
            throw ServiceException.PARSE_ERROR("unknown top-level rss element name: " + root.getQualifiedName(), null);
        }
        for (Element item : root.listElements("item")) {
            // get the item's date
            Date date = DateUtil.parseRFC2822Date(item.getAttribute("pubDate", null), null);
            if (date == null) {
                date = DateUtil.parseISO8601Date(item.getAttribute("date", null), dateChannel);
            }
            // construct an address for the author
            InternetAddress addr = addrChannel;
            try {
                addr = parseAuthor(item.getAttribute("author"));
            } catch (Exception e) {
                addr = parseDublinCreator(stripXML(item.getAttribute("creator", null)), addr);
            }
            // get the item's title and link, defaulting to the channel attributes
            String title = stripXML(item.getAttribute("title", subjChannel));
            String href = item.getAttribute("link", hrefChannel);
            String guid = item.getAttribute("guid", href);
            // make sure we haven't already seen this item
            if (fsd != null && fsd.alreadySeen(guid == hrefChannel ? null : guid, date == dateChannel ? null : date))
                continue;
            // handle the enclosure (associated media link), if any
            enclosures.clear();
            Element enc = item.getOptionalElement("enclosure");
            if (enc != null) {
                enclosures.add(new Enclosure(enc.getAttribute("url", null), null, enc.getAttribute("type", null)));
            }
            // get the feed item's content and guess at its type
            String text = item.getAttribute("encoded", null);
            boolean html = text != null;
            if (text == null) {
                text = item.getAttribute("description", null);
            }
            if (text == null) {
                text = item.getAttribute("abstract", null);
            }
            if (text == null && title != subjChannel) {
                text = "";
            }
            if (text == null)
                continue;
            html |= text.indexOf("</") != -1 || text.indexOf("/>") != -1 || text.indexOf("<p>") != -1;
            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 rss channel 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) URISyntaxException(java.net.URISyntaxException) MessagingException(javax.mail.MessagingException) HttpException(org.apache.http.HttpException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServiceException(com.zimbra.common.service.ServiceException) ParseException(javax.mail.internet.ParseException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress)

Example 40 with JavaMailInternetAddress

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

the class SpamHandler method handle.

public void handle(OperationContext octxt, Mailbox mbox, int itemId, MailItem.Type type, SpamReport report) throws ServiceException {
    Config config = Provisioning.getInstance().getConfig();
    String address;
    if (report.isSpam) {
        address = config.getSpamIsSpamAccount();
        if (Strings.isNullOrEmpty(address)) {
            log.debug("Spam address is not set.  Nothing to do.");
            return;
        }
    } else {
        address = config.getSpamIsNotSpamAccount();
        if (Strings.isNullOrEmpty(address)) {
            log.debug("Ham address is not set.  Nothing to do.");
            return;
        }
    }
    try {
        report.reportRecipient = new JavaMailInternetAddress(address, true);
    } catch (MessagingException e) {
        throw ServiceException.INVALID_REQUEST("Invalid address: " + address, e);
    }
    report.accountName = mbox.getAccount().getName();
    report.mailboxId = mbox.getId();
    if (octxt != null) {
        report.origIp = octxt.getRequestIP();
    }
    List<SpamReport> reports = Lists.newArrayList();
    switch(type) {
        case MESSAGE:
            report.messageId = itemId;
            reports.add(report);
            break;
        case CONVERSATION:
            for (Message msg : mbox.getMessagesByConversation(null, itemId)) {
                SpamReport msgReport = new SpamReport(report);
                msgReport.messageId = msg.getId();
                reports.add(report);
            }
            break;
        default:
            ZimbraLog.misc.warn("SpamHandler called on unhandled item type=" + type + " account=" + report.accountName + " id=" + itemId);
            return;
    }
    enqueue(reports);
}
Also used : Message(com.zimbra.cs.mailbox.Message) SMTPMessage(com.sun.mail.smtp.SMTPMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Config(com.zimbra.cs.account.Config) 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