Search in sources :

Example 36 with AddressException

use of javax.mail.internet.AddressException in project nhin-d by DirectProject.

the class RESTSmtpAgentConfig method buildDomains.

@Override
protected void buildDomains() {
    domains = new ArrayList<String>();
    domainPostmasters = new HashMap<String, DomainPostmaster>();
    // get the domain list first
    try {
        lookedupRESTServiceDomains = domainService.searchDomains("", null);
    } catch (Exception e) {
        throw new SmtpAgentException(SmtpAgentError.InvalidConfigurationFormat, "WebService error getting domains list: " + e.getMessage(), e);
    }
    if (lookedupRESTServiceDomains != null) {
        for (Domain dom : lookedupRESTServiceDomains) {
            domains.add(dom.getDomainName());
            try {
                String configuredAddress = (dom.getPostmasterAddress() == null) ? "" : dom.getPostmasterAddress().getEmailAddress();
                configuredAddress = (configuredAddress == null || configuredAddress.trim().isEmpty()) ? DomainPostmaster.getDefaultPostmaster(dom.getDomainName()) : configuredAddress;
                domainPostmasters.put(dom.getDomainName().toUpperCase(Locale.getDefault()), new DomainPostmaster(dom.getDomainName(), new InternetAddress(configuredAddress)));
            } catch (AddressException e) {
            }
        }
    }
    if (domains.size() == 0)
        throw new SmtpAgentException(SmtpAgentError.MissingDomains);
    // now get the trust anchors
    buildTrustAnchorResolver();
}
Also used : SmtpAgentException(org.nhindirect.gateway.smtp.SmtpAgentException) InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException) DomainPostmaster(org.nhindirect.gateway.smtp.DomainPostmaster) Domain(org.nhindirect.config.model.Domain) AddressException(javax.mail.internet.AddressException) SmtpAgentException(org.nhindirect.gateway.smtp.SmtpAgentException) PolicyParseException(org.nhindirect.policy.PolicyParseException)

Example 37 with AddressException

use of javax.mail.internet.AddressException in project nhin-d by DirectProject.

the class NHINDAddressCollection method parse.

/**
     * Parses an message router header to a collection of address.  The addressline may or may not include the header name.
     * @param addressesLine The raw message header.  The header name does not need to be included, but should use the proper header delimiter
     * if it is included.
     * @param source The address source type of the address line.
     * @return A collection of addresses parsed from the address line.
     */
public static NHINDAddressCollection parse(String addressesLine, AddressSource source) {
    NHINDAddressCollection retVal = new NHINDAddressCollection();
    if (addressesLine != null) {
        // strip the header separator if it exists
        int index = addressesLine.indexOf(':');
        String addressString = index > -1 ? addressesLine.substring(index + 1) : addressesLine;
        // split out the address using the standard delimiter
        //String[] addresses = addressString.split(String.valueOf(MailStandard.MailAddressSeparator));
        InternetAddress[] addresses = null;
        try {
            addresses = InternetAddress.parseHeader(addressString, true);
        } catch (AddressException e) {
            throw new NHINDException("Invalid email address format.", e);
        }
        for (InternetAddress addr : addresses) retVal.add(new NHINDAddress(addr.getAddress(), source));
    }
    return retVal;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException)

Example 38 with AddressException

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

the class ZEmailAddress method parseAddresses.

/**
    *
    * @param type type of addresses to create in the returned list.
    * @see com.zimbra.client.ZEmailAddress EMAIL_TYPE_TO, etc.
    * @return list of ZEMailAddress obejcts.
    * @throws ServiceException
    */
public static List<ZEmailAddress> parseAddresses(String line, String type) throws ServiceException {
    try {
        line = line.replace(";", ",");
        InternetAddress[] inetAddrs = JavaMailInternetAddress.parseHeader(line, false);
        List<ZEmailAddress> result = new ArrayList<ZEmailAddress>(inetAddrs.length);
        for (InternetAddress ia : inetAddrs) {
            result.add(new ZEmailAddress(ia.getAddress().replaceAll("\"", ""), null, ia.getPersonal(), type));
        }
        return result;
    } catch (AddressException e) {
        throw ServiceException.INVALID_REQUEST("Couldn't parse address", e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) AddressException(javax.mail.internet.AddressException) ArrayList(java.util.ArrayList)

Example 39 with AddressException

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

the class Notification method notifyIfNecessary.

/**
     * If the recipient's account is set up for email notification, sends a notification
     * message to the user's notification address.
     */
private void notifyIfNecessary(Account account, Message msg, String rcpt) throws MessagingException, ServiceException {
    // Does user have new mail notification turned on
    boolean notify = account.getBooleanAttr(Provisioning.A_zimbraPrefNewMailNotificationEnabled, false);
    if (!notify) {
        return;
    }
    // Validate notification address
    String destination = account.getAttr(Provisioning.A_zimbraPrefNewMailNotificationAddress);
    if (destination == null) {
        nfailed("destination not set", null, rcpt, msg, null);
        return;
    }
    try {
        new JavaMailInternetAddress(destination);
    } catch (AddressException ae) {
        nfailed("invalid destination", destination, rcpt, msg, ae);
        return;
    }
    // filter rules, we assume it's not interesting.
    if (msg.inSpam()) {
        nfailed("in spam", destination, rcpt, msg);
        return;
    }
    try {
        if (msg.inTrash()) {
            nfailed("in trash", destination, rcpt, msg);
            return;
        }
    } catch (ServiceException e) {
        nfailed("call to Message.inTrash() failed", destination, rcpt, msg, e);
        return;
    }
    // If precedence is bulk or junk
    MimeMessage mm = msg.getMimeMessage();
    String[] precedence = mm.getHeader("Precedence");
    if (hasPrecedence(precedence, "bulk")) {
        nfailed("precedence bulk", destination, rcpt, msg);
        return;
    }
    if (hasPrecedence(precedence, "junk")) {
        nfailed("precedence junk", destination, rcpt, msg);
        return;
    }
    // Check for mail loop
    String[] autoSubmittedHeaders = mm.getHeader("Auto-Submitted");
    if (autoSubmittedHeaders != null) {
        for (int i = 0; i < autoSubmittedHeaders.length; i++) {
            String headerValue = autoSubmittedHeaders[i].toLowerCase();
            if (headerValue.indexOf("notification") != -1) {
                nfailed("detected a mail loop", destination, rcpt, msg);
                return;
            }
        }
    }
    // Send the message
    try {
        Session smtpSession = JMSession.getSmtpSession();
        // Assemble message components
        MimeMessage out = assembleNotificationMessage(account, msg, rcpt, destination, smtpSession);
        if (out == null) {
            return;
        }
        String envFrom = "<>";
        try {
            if (!Provisioning.getInstance().getConfig().getBooleanAttr(Provisioning.A_zimbraAutoSubmittedNullReturnPath, true)) {
                envFrom = account.getName();
            }
        } catch (ServiceException se) {
            ZimbraLog.mailbox.warn("error encoutered looking up return path configuration, using null return path instead", se);
        }
        smtpSession.getProperties().setProperty("mail.smtp.from", envFrom);
        Transport.send(out);
        ZimbraLog.mailbox.info("notification sent dest='" + destination + "' rcpt='" + rcpt + "' mid=" + msg.getId());
    } catch (MessagingException me) {
        nfailed("send failed", destination, rcpt, msg, me);
    }
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session)

Example 40 with AddressException

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

the class ScheduleOutbox method handleEventRequest.

private void handleEventRequest(DavContext ctxt, ZCalendar.ZVCalendar cal, ZComponent req, DelegationInfo delegationInfo, String rcpt, Element resp) throws ServiceException, DavException {
    if (!DavResource.isSchedulingEnabled()) {
        resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
        resp.addElement(DavElements.E_REQUEST_STATUS).setText("5.3;No scheduling for the user");
        return;
    }
    ArrayList<Address> recipients = new java.util.ArrayList<Address>();
    InternetAddress from, sender, to;
    Account target = null;
    try {
        sender = new JavaMailInternetAddress(delegationInfo.getOriginatorEmail());
        Provisioning prov = Provisioning.getInstance();
        if (ctxt.getActingAsDelegateFor() != null) {
            target = prov.getAccountByName(ctxt.getActingAsDelegateFor());
        }
        if (target != null) {
            from = AccountUtil.getFriendlyEmailAddress(target);
        } else {
            if (delegationInfo.getOwnerEmail() != null) {
                from = new JavaMailInternetAddress(delegationInfo.getOwnerEmail());
            } else {
                target = getMailbox(ctxt).getAccount();
                if (AccountUtil.addressMatchesAccount(target, delegationInfo.getOriginatorEmail())) {
                    // Make sure we don't use two different aliases for From and Sender.
                    // This is a concern with Apple iCal, which picks a random alias as originator.
                    from = sender;
                } else {
                    from = AccountUtil.getFriendlyEmailAddress(target);
                }
            }
        }
        if (sender.getAddress() != null && sender.getAddress().equalsIgnoreCase(from.getAddress())) {
            sender = null;
        }
        to = new JavaMailInternetAddress(CalDavUtils.stripMailto(rcpt));
        recipients.add(to);
    } catch (AddressException e) {
        resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
        resp.addElement(DavElements.E_REQUEST_STATUS).setText("3.7;" + rcpt);
        return;
    }
    String status = req.getPropVal(ICalTok.STATUS, "");
    String method = cal.getPropVal(ICalTok.METHOD, "REQUEST");
    String subject = "";
    if (method.equals("REQUEST")) {
        ZProperty organizerProp = req.getProperty(ICalTok.ORGANIZER);
        if (organizerProp != null) {
            String organizerStr = this.getAddressFromPrincipalURL(new ZOrganizer(organizerProp).getAddress());
            if (!AccountUtil.addressMatchesAccount(getMailbox(ctxt).getAccount(), organizerStr)) {
                ZimbraLog.dav.debug("scheduling appointment on behalf of %s", organizerStr);
            }
        }
    } else if (method.equals("REPLY")) {
        ZProperty attendeeProp = req.getProperty(ICalTok.ATTENDEE);
        if (attendeeProp == null)
            throw new DavException("missing property ATTENDEE", HttpServletResponse.SC_BAD_REQUEST);
        ZAttendee attendee = new ZAttendee(attendeeProp);
        String partStat = attendee.getPartStat();
        if (partStat.equals(IcalXmlStrMap.PARTSTAT_ACCEPTED)) {
            subject = "Accept: ";
        } else if (partStat.equals(IcalXmlStrMap.PARTSTAT_TENTATIVE)) {
            subject = "Tentative: ";
        } else if (partStat.equals(IcalXmlStrMap.PARTSTAT_DECLINED)) {
            subject = "Decline: ";
        }
    }
    if (status.equals("CANCELLED"))
        subject = "Cancelled: ";
    subject += req.getPropVal(ICalTok.SUMMARY, "");
    String uid = req.getPropVal(ICalTok.UID, null);
    if (uid == null) {
        resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
        resp.addElement(DavElements.E_REQUEST_STATUS).setText("3.1;UID");
        return;
    }
    try {
        List<Invite> components = Invite.createFromCalendar(ctxt.getAuthAccount(), null, cal, false);
        FriendlyCalendaringDescription friendlyDesc = new FriendlyCalendaringDescription(components, ctxt.getAuthAccount());
        String desc = friendlyDesc.getAsPlainText();
        String descHtml = req.getDescriptionHtml();
        if ((descHtml == null) || (descHtml.length() == 0))
            descHtml = friendlyDesc.getAsHtml();
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(ctxt.getAuthAccount());
        MimeMessage mm = CalendarMailSender.createCalendarMessage(target, from, sender, recipients, subject, desc, descHtml, uid, cal);
        mbox.getMailSender().setSendPartial(true).sendMimeMessage(ctxt.getOperationContext(), mbox, true, mm, null, null, null, null, false);
    } catch (ServiceException e) {
        resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
        resp.addElement(DavElements.E_REQUEST_STATUS).setText("5.1");
        return;
    }
    resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
    resp.addElement(DavElements.E_REQUEST_STATUS).setText("2.0;Success");
}
Also used : 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) DavException(com.zimbra.cs.dav.DavException) ArrayList(java.util.ArrayList) ZOrganizer(com.zimbra.cs.mailbox.calendar.ZOrganizer) Provisioning(com.zimbra.cs.account.Provisioning) FriendlyCalendaringDescription(com.zimbra.cs.mailbox.calendar.FriendlyCalendaringDescription) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) ZAttendee(com.zimbra.cs.mailbox.calendar.ZAttendee) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Aggregations

AddressException (javax.mail.internet.AddressException)46 InternetAddress (javax.mail.internet.InternetAddress)37 MimeMessage (javax.mail.internet.MimeMessage)18 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)17 MessagingException (javax.mail.MessagingException)15 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 Date (java.util.Date)7 Address (javax.mail.Address)7 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 Properties (java.util.Properties)4 Session (javax.mail.Session)4 MimeBodyPart (javax.mail.internet.MimeBodyPart)4 ServiceException (com.zimbra.common.service.ServiceException)3 Account (com.zimbra.cs.account.Account)3 Invite (com.zimbra.cs.mailbox.calendar.Invite)3 ZAttendee (com.zimbra.cs.mailbox.calendar.ZAttendee)3 Locale (java.util.Locale)3