Search in sources :

Example 26 with Address

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

the class MimeTest method semiColonAddressSeparator.

@Test
public void semiColonAddressSeparator() throws Exception {
    StringBuilder to = new StringBuilder("To: ");
    int count = 4;
    for (int i = 1; i < count + 1; i++) {
        to.append("user").append(count).append("@example.com").append(";");
    }
    to.setLength(to.length() - 1);
    to.append("\r\n");
    String content = "From: user1@example.com\r\n" + to.toString() + "Subject: test\r\n" + "Content-Type: test/plain\r\n\r\n" + "test message";
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    Address[] recipients = mm.getAllRecipients();
    Assert.assertEquals(count, recipients.length);
}
Also used : Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 27 with Address

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

the class AutoProvision method sendNotifMessage.

protected void sendNotifMessage(Account acct, String password) throws ServiceException {
    String subject = fillTemplate(acct, domain.getAutoProvNotificationSubject());
    String body = fillTemplate(acct, domain.getAutoProvNotificationBody());
    String from = domain.getAutoProvNotificationFromAddress();
    if (from == null) {
        // TODO: should we use a seperate boolean control?
        return;
    }
    String toAddr = acct.getName();
    try {
        SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
        InternetAddress addr = null;
        try {
            addr = new JavaMailInternetAddress(from);
        } catch (AddressException e) {
            // log and try the next one
            ZimbraLog.autoprov.warn("invalid address in " + Provisioning.A_zimbraAutoProvNotificationFromAddress, e);
        }
        Address fromAddr = addr;
        Address replyToAddr = addr;
        // From
        out.setFrom(fromAddr);
        // Reply-To
        out.setReplyTo(new Address[] { replyToAddr });
        // To
        out.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(toAddr));
        // Date
        out.setSentDate(new Date());
        // Subject
        Locale locale = acct.getLocale();
        out.setSubject(subject);
        // NOTIFY=NEVER
        out.setNotifyOptions(SMTPMessage.NOTIFY_NEVER);
        // body
        MimeMultipart mmp = new ZMimeMultipart("alternative");
        // TEXT part (add me first!)
        String text = body;
        MimeBodyPart textPart = new ZMimeBodyPart();
        textPart.setText(text, MimeConstants.P_CHARSET_UTF8);
        mmp.addBodyPart(textPart);
        // HTML part
        StringBuilder html = new StringBuilder();
        html.append("<h4>\n");
        html.append("<p>" + body + "</p>\n");
        html.append("</h4>\n");
        html.append("\n");
        MimeBodyPart htmlPart = new ZMimeBodyPart();
        htmlPart.setDataHandler(new DataHandler(new HtmlPartDataSource(html.toString())));
        mmp.addBodyPart(htmlPart);
        out.setContent(mmp);
        // send it
        Transport.send(out);
        // log
        Address[] rcpts = out.getRecipients(javax.mail.Message.RecipientType.TO);
        StringBuilder rcptAddr = new StringBuilder();
        for (Address a : rcpts) rcptAddr.append(a.toString());
        ZimbraLog.autoprov.info("auto provision notification sent rcpt='" + rcptAddr + "' Message-ID=" + out.getMessageID());
    } catch (MessagingException e) {
        ZimbraLog.autoprov.warn("send auto provision notification failed rcpt='" + toAddr + "'", e);
    }
}
Also used : Locale(java.util.Locale) SMTPMessage(com.sun.mail.smtp.SMTPMessage) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Address(javax.mail.Address) EmailAddress(com.zimbra.cs.account.names.NameUtil.EmailAddress) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) DataHandler(javax.activation.DataHandler) Date(java.util.Date) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) AddressException(javax.mail.internet.AddressException) 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 28 with Address

use of javax.mail.Address in project ats-framework by Axway.

the class MimePackage method getRecipients.

/**
     * Get the recipients of the specified type
     *
     * @param recipientType
     *            the type of recipient - to, cc or bcc
     * @return array with recipients, emtpy array of no recipients of this type
     *         are present
     * @throws PackageException
     */
@PublicAtsApi
public String[] getRecipients(RecipientType recipientType) throws PackageException {
    try {
        Address[] recipientAddresses = message.getRecipients(recipientType.toJavamailType());
        // return an empty string if no recipients are present
        if (recipientAddresses == null) {
            return new String[] {};
        }
        String[] recipients = new String[recipientAddresses.length];
        for (int i = 0; i < recipientAddresses.length; i++) {
            recipients[i] = recipientAddresses[i].toString();
        }
        return recipients;
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 29 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)

Example 30 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)

Aggregations

Address (javax.mail.Address)86 InternetAddress (javax.mail.internet.InternetAddress)66 MessagingException (javax.mail.MessagingException)34 MimeMessage (javax.mail.internet.MimeMessage)32 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)21 ArrayList (java.util.ArrayList)19 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 ItemId (com.zimbra.cs.service.util.ItemId)8 Header (javax.mail.Header)8 NHINDAddress (org.nhindirect.stagent.NHINDAddress)8 Message (javax.mail.Message)7 MimeMultipart (javax.mail.internet.MimeMultipart)7 ZVCalendar (com.zimbra.common.calendar.ZCalendar.ZVCalendar)6 Invite (com.zimbra.cs.mailbox.calendar.Invite)6 Properties (java.util.Properties)6