Search in sources :

Example 41 with Address

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

the class CalendarRequest method notifyCalendarItem.

// Notify attendees following an update to the series of a recurring appointment.  Only the
// added attendees are notified if notifyAllAttendees is false.  If it is true all attendees
// for each invite are notified.  (Some invites may have more attendees than others.)
protected static void notifyCalendarItem(ZimbraSoapContext zsc, OperationContext octxt, Account acct, Mailbox mbox, CalendarItem calItem, boolean notifyAllAttendees, List<ZAttendee> addedAttendees, boolean ignorePastExceptions, MailSendQueue sendQueue) throws ServiceException {
    boolean onBehalfOf = isOnBehalfOfRequest(zsc);
    Account authAcct = getAuthenticatedAccount(zsc);
    boolean hidePrivate = !calItem.isPublic() && !calItem.allowPrivateAccess(authAcct, zsc.isUsingAdminPrivileges());
    Address from = AccountUtil.getFriendlyEmailAddress(acct);
    Address sender = null;
    if (onBehalfOf)
        sender = AccountUtil.getFriendlyEmailAddress(authAcct);
    List<Address> addedRcpts = CalendarMailSender.toListFromAttendees(addedAttendees);
    long now = octxt != null ? octxt.getTimestamp() : System.currentTimeMillis();
    mbox.lock.lock();
    try {
        // Refresh the cal item so we see the latest blob, whose path may have been changed
        // earlier in the current request.
        calItem = mbox.getCalendarItemById(octxt, calItem.getId());
        Invite[] invites = calItem.getInvites();
        // Get exception instances.  These will be included in the series update email.
        List<Invite> exceptions = new ArrayList<Invite>();
        for (Invite inv : invites) {
            if (inv.hasRecurId()) {
                exceptions.add(inv);
            }
        }
        // Send the update invites.
        boolean didExceptions = false;
        for (Invite inv : invites) {
            if (ignorePastExceptions && inv.hasRecurId() && !inviteIsAfterTime(inv, now)) {
                continue;
            }
            // Make the new iCalendar part to send.
            ZVCalendar cal = inv.newToICalendar(!hidePrivate);
            // For series invite, append the exception instances.
            if (inv.isRecurrence() && !didExceptions) {
                // Find the VEVENT/VTODO for the series.
                ZComponent seriesComp = null;
                for (Iterator<ZComponent> compIter = cal.getComponentIterator(); compIter.hasNext(); ) {
                    ZComponent comp = compIter.next();
                    ICalTok compName = comp.getTok();
                    if (ICalTok.VEVENT.equals(compName) || ICalTok.VTODO.equals(compName)) {
                        if (comp.getProperty(ICalTok.RRULE) != null) {
                            seriesComp = comp;
                            break;
                        }
                    }
                }
                for (Invite except : exceptions) {
                    if (except.isCancel() && seriesComp != null) {
                        // Cancels are added as EXDATEs in the series VEVENT/VTODO.
                        RecurId rid = except.getRecurId();
                        if (rid != null && rid.getDt() != null) {
                            ZProperty exdate = rid.getDt().toProperty(ICalTok.EXDATE, false);
                            seriesComp.addProperty(exdate);
                        }
                    } else {
                        // Exception instances are added as additional VEVENTs/VTODOs.
                        ZComponent exceptComp = except.newToVComponent(false, !hidePrivate);
                        cal.addComponent(exceptComp);
                    }
                }
                didExceptions = true;
            }
            // Compose email using the existing MimeMessage as template and send it.
            MimeMessage mmInv = calItem.getSubpartMessage(inv.getMailItemId());
            List<Address> rcpts;
            if (notifyAllAttendees) {
                rcpts = CalendarMailSender.toListFromAttendees(inv.getAttendees());
            } else {
                rcpts = addedRcpts;
            }
            if (rcpts != null && !rcpts.isEmpty()) {
                MimeMessage mmModify = CalendarMailSender.createCalendarMessage(authAcct, from, sender, rcpts, mmInv, inv, cal, true);
                CalSendData csd = new CalSendData();
                csd.mMm = mmModify;
                csd.mOrigId = new ItemId(mbox, inv.getMailItemId());
                MailSendQueueEntry entry = new MailSendQueueEntry(octxt, mbox, csd, null);
                sendQueue.add(entry);
            }
        }
    } finally {
        mbox.lock.release();
    }
}
Also used : Account(com.zimbra.cs.account.Account) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) ArrayList(java.util.ArrayList) RecurId(com.zimbra.cs.mailbox.calendar.RecurId) ItemId(com.zimbra.cs.service.util.ItemId) ICalTok(com.zimbra.common.calendar.ZCalendar.ICalTok) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 42 with Address

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

the class CalendarRequest method notifyRemovedAttendees.

protected static void notifyRemovedAttendees(ZimbraSoapContext zsc, OperationContext octxt, Account acct, Mailbox mbox, CalendarItem calItem, Invite invToCancel, List<ZAttendee> removedAttendees, MailSendQueue sendQueue) throws ServiceException {
    boolean onBehalfOf = isOnBehalfOfRequest(zsc);
    Account authAcct = getAuthenticatedAccount(zsc);
    Locale locale = !onBehalfOf ? acct.getLocale() : authAcct.getLocale();
    CalSendData dat = new CalSendData();
    dat.mOrigId = new ItemId(mbox, invToCancel.getMailItemId());
    dat.mReplyType = MailSender.MSGTYPE_REPLY;
    String text = L10nUtil.getMessage(MsgKey.calendarCancelRemovedFromAttendeeList, locale);
    if (ZimbraLog.calendar.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder("Sending cancellation message for \"");
        sb.append(invToCancel.getName()).append("\" to ");
        sb.append(getAttendeesAddressList(removedAttendees));
        ZimbraLog.calendar.debug(sb.toString());
    }
    List<Address> rcpts = CalendarMailSender.toListFromAttendees(removedAttendees);
    try {
        dat.mInvite = CalendarUtils.buildCancelInviteCalendar(acct, authAcct, zsc.isUsingAdminPrivileges(), onBehalfOf, calItem, invToCancel, text, removedAttendees);
        ZVCalendar cal = dat.mInvite.newToICalendar(true);
        dat.mMm = CalendarMailSender.createCancelMessage(acct, authAcct, zsc.isUsingAdminPrivileges(), onBehalfOf, rcpts, calItem, invToCancel, text, cal);
        // If we are sending this cancellation to other people, then we MUST be the organizer!
        if (!dat.mInvite.isOrganizer() && rcpts != null && !rcpts.isEmpty())
            throw MailServiceException.MUST_BE_ORGANIZER("updateRemovedInvitees");
        sendCalendarCancelMessage(zsc, octxt, calItem.getFolderId(), acct, mbox, dat, false, sendQueue);
    } catch (ServiceException ex) {
        String to = getAttendeesAddressList(removedAttendees);
        ZimbraLog.calendar.debug("Could not inform attendees (" + to + ") that they were removed from meeting " + invToCancel.toString() + " b/c of exception: " + ex.toString());
    }
}
Also used : Locale(java.util.Locale) Account(com.zimbra.cs.account.Account) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ItemId(com.zimbra.cs.service.util.ItemId)

Example 43 with Address

use of javax.mail.Address in project logging-log4j2 by apache.

the class SmtpAppenderTest method testMessageFactorySetFrom.

@Test
public void testMessageFactorySetFrom() throws MessagingException {
    final MimeMessageBuilder builder = new MimeMessageBuilder(null);
    final String address = "testing@example.com";
    assertNull(builder.build().getFrom());
    builder.setFrom(null);
    Address[] array = null;
    final Address addr = InternetAddress.getLocalAddress(null);
    if (addr != null) {
        array = new Address[] { addr };
    }
    assertArrayEquals(array, builder.build().getFrom());
    builder.setFrom(address);
    assertArrayEquals(new Address[] { new InternetAddress(address) }, builder.build().getFrom());
}
Also used : MimeMessageBuilder(org.apache.logging.log4j.core.net.MimeMessageBuilder) InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) Test(org.junit.Test)

Example 44 with Address

use of javax.mail.Address in project opennms by OpenNMS.

the class JavaMailAckReaderIT method workingWithSimpleTextMessages.

/**
 * tests the ability to create acknowledgments from an email for plain text.  This test
 * creates a message from scratch rather than reading from an inbox.
 */
@Test
public void workingWithSimpleTextMessages() {
    Properties props = new Properties();
    Message msg = new MimeMessage(Session.getDefaultInstance(props));
    try {
        Address[] addrs = new Address[1];
        addrs[0] = new InternetAddress("david@opennms.org");
        msg.addFrom(addrs);
        msg.addRecipient(javax.mail.internet.MimeMessage.RecipientType.TO, addrs[0]);
        msg.setSubject("Re: Notice #1234 JavaMailReaderImplTest Test Message");
        msg.setText("ACK");
    } catch (AddressException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    }
    List<Message> msgs = new ArrayList<Message>(1);
    msgs.add(msg);
    List<OnmsAcknowledgment> acks = m_processor.createAcks(msgs);
    Assert.assertEquals(1, acks.size());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(0).getAckType());
    Assert.assertEquals("david@opennms.org", acks.get(0).getAckUser());
    Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(0).getAckAction());
    Assert.assertEquals(new Integer(1234), acks.get(0).getRefId());
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) OnmsAcknowledgment(org.opennms.netmgt.model.OnmsAcknowledgment) Message(javax.mail.Message) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) Test(org.junit.Test)

Example 45 with Address

use of javax.mail.Address in project opennms by OpenNMS.

the class JavaMailAckReaderIT method workingWithMultiPartMessages.

/**
 * tests the ability to create acknowledgments from an email for a multi-part text.  This test
 * creates a message from scratch rather than reading from an inbox.  This message creation
 * may not actually represent what comes from a mail server.
 */
@Test
public void workingWithMultiPartMessages() throws JavaMailerException, MessagingException {
    List<Message> msgs = new ArrayList<>();
    Properties props = new Properties();
    Message msg = new MimeMessage(Session.getDefaultInstance(props));
    Address[] addrs = new Address[1];
    addrs[0] = new InternetAddress("david@opennms.org");
    msg.addFrom(addrs);
    msg.addRecipient(RecipientType.TO, new InternetAddress("david@opennms.org"));
    msg.setSubject("Re: Notice #1234 JavaMailReaderImplTest Test Message");
    Multipart mpContent = new MimeMultipart();
    BodyPart textBp = new MimeBodyPart();
    BodyPart htmlBp = new MimeBodyPart();
    textBp.setText("ack");
    htmlBp.setContent("<html>\n" + " <head>\n" + "  <title>\n" + "   Acknowledge\n" + "  </title>\n" + " </head>\n" + " <body>\n" + "  <h1>\n" + "   ack\n" + "  </h1>\n" + " </body>\n" + "</html>", "text/html");
    mpContent.addBodyPart(textBp);
    mpContent.addBodyPart(htmlBp);
    msg.setContent(mpContent);
    msgs.add(msg);
    List<OnmsAcknowledgment> acks = m_processor.createAcks(msgs);
    Assert.assertEquals(1, acks.size());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(0).getAckType());
    Assert.assertEquals("david@opennms.org", acks.get(0).getAckUser());
    Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(0).getAckAction());
    Assert.assertEquals(new Integer(1234), acks.get(0).getRefId());
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) OnmsAcknowledgment(org.opennms.netmgt.model.OnmsAcknowledgment) Message(javax.mail.Message) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) ArrayList(java.util.ArrayList) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

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