Search in sources :

Example 96 with InternetAddress

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

the class DefaultTnefToICalendar method addAttendees.

/**
     * 
     * @param icalOutput
     * @param mimeMsg
     * @param partstat
     * @param replyWanted
     * @throws ParserException
     * @throws URISyntaxException
     * @throws IOException
     * @throws ParseException
     * @throws MessagingException
     */
private void addAttendees(ContentHandler icalOutput, MimeMessage mimeMsg, PartStat partstat, boolean replyWanted) throws ParserException, URISyntaxException, IOException, ParseException, MessagingException {
    // ATTENDEEs
    InternetAddress firstFromIA = null;
    String firstFromEmailAddr = null;
    // Use for SENT-BY if applicable
    String senderMailto = null;
    String senderCn = null;
    javax.mail.Address[] toRecips = null;
    javax.mail.Address[] ccRecips = null;
    javax.mail.Address[] bccRecips = null;
    javax.mail.Address[] msgFroms = null;
    javax.mail.Address msgSender = null;
    if (mimeMsg != null) {
        toRecips = mimeMsg.getRecipients(javax.mail.Message.RecipientType.TO);
        ccRecips = mimeMsg.getRecipients(javax.mail.Message.RecipientType.CC);
        bccRecips = mimeMsg.getRecipients(javax.mail.Message.RecipientType.BCC);
        msgFroms = mimeMsg.getFrom();
        msgSender = mimeMsg.getSender();
    }
    if (msgFroms != null) {
        if (msgFroms.length != 1) {
            sLog.debug(msgFroms.length + " From: recipients for " + method.getValue());
        }
        if (msgFroms.length >= 1) {
            firstFromIA = (InternetAddress) msgFroms[0];
            firstFromEmailAddr = firstFromIA.getAddress();
        }
        if (msgSender != null) {
            String senderAddr = msgSender.toString();
            if (msgSender instanceof InternetAddress) {
                InternetAddress senderIA = (InternetAddress) msgSender;
                senderAddr = senderIA.getAddress();
                senderCn = senderIA.getPersonal();
                if (!firstFromIA.equals(senderIA)) {
                    senderMailto = "Mailto:" + senderAddr;
                }
            }
        }
    }
    if (method.equals(Method.REPLY) || method.equals(Method.COUNTER)) {
        // from ATTENDEE to ORGANIZER
        if (toRecips != null) {
            if (toRecips.length != 1) {
                sLog.debug(toRecips.length + " To: recipients for " + method.getValue());
            }
            if (toRecips.length >= 1) {
                InternetAddress ia = (InternetAddress) toRecips[0];
                String email = ia.getAddress();
                String displayName = ia.getPersonal();
                icalOutput.startProperty(Property.ORGANIZER);
                icalOutput.propertyValue("Mailto:" + email);
                if (displayName != null) {
                    icalOutput.parameter(Parameter.CN, displayName);
                }
                icalOutput.endProperty(Property.ORGANIZER);
            }
        }
        if (firstFromEmailAddr != null) {
            String displayName = firstFromIA.getPersonal();
            icalOutput.startProperty(Property.ATTENDEE);
            icalOutput.propertyValue("Mailto:" + firstFromEmailAddr);
            if (displayName != null) {
                icalOutput.parameter(Parameter.CN, displayName);
            }
            icalOutput.parameter(Parameter.CUTYPE, CuType.INDIVIDUAL.getValue());
            if (partstat != null) {
                icalOutput.parameter(Parameter.PARTSTAT, partstat.getValue());
            }
            if (senderMailto != null) {
                icalOutput.parameter(Parameter.SENT_BY, senderMailto);
            }
            icalOutput.endProperty(Property.ATTENDEE);
        }
    } else {
        // ORGANIZER to ATTENDEEs - REQUEST or CANCEL
        InternetAddress organizerEmail = null;
        if (firstFromEmailAddr != null) {
            SentBy sentBy = null;
            Cn cn = null;
            if (senderMailto != null) {
                sentBy = new SentBy(senderMailto);
            }
            organizerEmail = firstFromIA;
            String displayName = firstFromIA.getPersonal();
            if ((displayName != null) && (!displayName.equals(firstFromEmailAddr))) {
                cn = new Cn(displayName);
            }
            Organizer organizer = new Organizer();
            organizer.setValue("Mailto:" + firstFromEmailAddr);
            if (cn != null) {
                organizer.getParameters().add(cn);
            }
            if (sentBy != null) {
                organizer.getParameters().add(sentBy);
            }
            IcalUtil.addProperty(icalOutput, organizer);
            if (icalType == ICALENDAR_TYPE.VEVENT) {
                // Assumption - ORGANIZER is an attendee and is attending.
                Attendee attendee = new Attendee("Mailto:" + firstFromEmailAddr);
                if (cn != null) {
                    attendee.getParameters().add(cn);
                }
                attendee.getParameters().add(CuType.INDIVIDUAL);
                attendee.getParameters().add(Role.REQ_PARTICIPANT);
                if (!method.equals(Method.CANCEL)) {
                    PartStat orgPartstat = PartStat.ACCEPTED;
                    if (ccRecips != null) {
                        for (Address a : ccRecips) {
                            InternetAddress ia = (InternetAddress) a;
                            if (organizerEmail.equals(ia)) {
                                orgPartstat = PartStat.TENTATIVE;
                                break;
                            }
                        }
                    }
                    attendee.getParameters().add(orgPartstat);
                }
                // Was including SENT-BY but probably not appropriate
                // for a request
                IcalUtil.addProperty(icalOutput, attendee);
            }
        }
        if (toRecips != null) {
            for (Address a : toRecips) {
                InternetAddress ia = (InternetAddress) a;
                if ((organizerEmail != null) && organizerEmail.equals(ia)) {
                    // No need to add the information twice
                    continue;
                }
                addAttendee(icalOutput, ia, Role.REQ_PARTICIPANT, CuType.INDIVIDUAL, partstat, replyWanted);
            }
        }
        if (ccRecips != null) {
            for (Address a : ccRecips) {
                InternetAddress ia = (InternetAddress) a;
                if ((organizerEmail != null) && organizerEmail.equals(ia)) {
                    // No need to add the information twice
                    continue;
                }
                addAttendee(icalOutput, ia, Role.OPT_PARTICIPANT, CuType.INDIVIDUAL, partstat, replyWanted);
            }
        }
        if (bccRecips != null) {
            for (Address a : bccRecips) {
                InternetAddress ia = (InternetAddress) a;
                addAttendee(icalOutput, ia, Role.NON_PARTICIPANT, CuType.RESOURCE, partstat, replyWanted);
            }
        }
    }
    if (senderMailto != null) {
        XProperty msOlkSender = new XProperty("X-MS-OLK-SENDER", senderMailto);
        if (senderCn != null) {
            Cn cn = new Cn(senderCn);
            msOlkSender.getParameters().add(cn);
        }
        IcalUtil.addProperty(icalOutput, msOlkSender);
    }
}
Also used : Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) XProperty(net.fortuna.ical4j.model.property.XProperty) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) Organizer(net.fortuna.ical4j.model.property.Organizer) SentBy(net.fortuna.ical4j.model.parameter.SentBy) PartStat(net.fortuna.ical4j.model.parameter.PartStat) Cn(net.fortuna.ical4j.model.parameter.Cn) Attendee(net.fortuna.ical4j.model.property.Attendee)

Example 97 with InternetAddress

use of javax.mail.internet.InternetAddress in project adempiere by adempiere.

the class ConfigurationData method testMail.

//	testAdempiere
/**************************************************************************
	 * 	Test (optional) Mail
	 *	@return error message or null, if OK
	 */
private String testMail() {
    //	Mail Server
    String server = p_panel != null ? p_panel.fMailServer.getText() : (String) p_properties.get(ADEMPIERE_MAIL_SERVER);
    boolean pass = server != null && server.length() > 0 && server.toLowerCase().indexOf("localhost") == -1 && !server.equals("127.0.0.1");
    String error = "Error Mail Server = " + server;
    InetAddress mailServer = null;
    try {
        if (pass)
            mailServer = InetAddress.getByName(server);
    } catch (Exception e) {
        error += " - " + e.getMessage();
        pass = false;
    }
    if (p_panel != null)
        p_panel.signalOK(p_panel.okMailServer, "ErrorMailServer", pass, true, error);
    if (!pass) {
        p_properties.setProperty(ADEMPIERE_MAIL_SERVER, "");
        return error;
    }
    p_properties.setProperty(ADEMPIERE_MAIL_SERVER, mailServer.getHostName());
    //	FR [ 402 ]
    //	Mail Port
    String mailPort = p_panel != null ? p_panel.fMailPort.getText() : (String) p_properties.get(ADEMPIERE_MAIL_PORT);
    int port = 25;
    if (mailPort != null) {
        try {
            port = Integer.parseInt(mailPort);
        } catch (Exception e) {
        }
    }
    //	Mail Protocol
    String mailProtocol = getProtocol();
    //	Mail Encryption Type
    String mailEncryptionType = getEncryptionType();
    //	Mail Authentication Mechanism
    String mailAuthMechanism = getAuthMechanism();
    //	Mail User
    String mailUser = p_panel != null ? p_panel.fMailUser.getText() : (String) p_properties.get(ADEMPIERE_MAIL_USER);
    String mailPassword = p_panel != null ? new String(p_panel.fMailPassword.getPassword()) : (String) p_properties.get(ADEMPIERE_MAIL_PASSWORD);
    //	log.config("Mail User = " + mailUser + "/" + mailPassword);
    //	Mail Address
    String adminEMailString = p_panel != null ? p_panel.fAdminEMail.getText() : (String) p_properties.get(ADEMPIERE_ADMIN_EMAIL);
    InternetAddress adminEMail = null;
    try {
        adminEMail = new InternetAddress(adminEMailString);
    } catch (Exception e) {
        error = "Not valid: " + adminEMailString + " - " + e.getMessage();
        pass = false;
    }
    //
    if (pass) {
        error = "Not verified EMail = " + adminEMail;
        pass = testMailServer(mailServer, adminEMail, mailUser, mailPassword, port, mailProtocol, mailEncryptionType, mailAuthMechanism);
    }
    if (p_panel != null)
        p_panel.signalOK(p_panel.okMailUser, "ErrorMail", pass, false, error);
    if (pass) {
        log.info("OK: EMail = " + adminEMail);
        p_properties.setProperty(ADEMPIERE_ADMIN_EMAIL, adminEMail.toString());
        p_properties.setProperty(ADEMPIERE_MAIL_USER, mailUser);
        p_properties.setProperty(ADEMPIERE_MAIL_PASSWORD, mailPassword);
        p_properties.setProperty(ADEMPIERE_MAIL_UPDATED, "No");
        p_properties.setProperty(ADEMPIERE_MAIL_PORT, mailPort);
        p_properties.setProperty(ADEMPIERE_MAIL_PT, mailProtocol);
        p_properties.setProperty(ADEMPIERE_MAIL_ET, mailEncryptionType);
        p_properties.setProperty(ADEMPIERE_MAIL_AM, mailAuthMechanism);
    } else {
        log.warning(error);
        p_properties.setProperty(ADEMPIERE_ADMIN_EMAIL, "");
        p_properties.setProperty(ADEMPIERE_MAIL_USER, "");
        p_properties.setProperty(ADEMPIERE_MAIL_PASSWORD, "");
        p_properties.setProperty(ADEMPIERE_MAIL_UPDATED, "");
        p_properties.setProperty(ADEMPIERE_MAIL_PORT, "");
        p_properties.setProperty(ADEMPIERE_MAIL_PT, "");
        p_properties.setProperty(ADEMPIERE_MAIL_ET, "");
        p_properties.setProperty(ADEMPIERE_MAIL_AM, "");
    }
    return null;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) InetAddress(java.net.InetAddress) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 98 with InternetAddress

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

the class ImapMessage method naddresses.

private static void naddresses(PrintStream ps, InternetAddress[] addrs) {
    int count = 0;
    if (addrs != null && addrs.length > 0) {
        for (InternetAddress addr : addrs) {
            if (addr.isGroup()) {
                //         group marker, and the mailbox name field holds the group name phrase."
                try {
                    String serialized = addr.getAddress();
                    int colon = serialized.indexOf(':');
                    String name = colon == -1 ? serialized : serialized.substring(0, colon);
                    InternetAddress[] members = addr.getGroup(false);
                    if (count++ == 0) {
                        ps.write('(');
                    }
                    ps.print("(NIL NIL ");
                    nstring(ps, name);
                    ps.print(" NIL)");
                    if (members != null) {
                        for (InternetAddress member : members) {
                            address(ps, member);
                        }
                    }
                    ps.print("(NIL NIL NIL NIL)");
                } catch (ParseException e) {
                }
            } else if (addr.getAddress() == null) {
                continue;
            } else {
                //         name, [SMTP] at-domain-list (source route), mailbox name, and host name."
                if (count++ == 0) {
                    ps.write('(');
                }
                address(ps, addr);
            }
        }
    }
    if (count == 0) {
        ps.write(NIL, 0, 3);
    } else {
        ps.write(')');
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) ParseException(javax.mail.internet.ParseException)

Example 99 with InternetAddress

use of javax.mail.internet.InternetAddress 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)

Example 100 with InternetAddress

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

the class ParsedMessage method getSenderEmail.

/**
     * If fromFirst is true, returns the email address of the first <tt>From</tt>
     * or <tt>Sender</tt> header.  If fromFirst is false, returns the email address
     * of the first <tt>Sender</tt> or <tt>From</tt> header.
     */
public String getSenderEmail(boolean fromFirst) {
    try {
        if (fromFirst) {
            // From header first, then Sender
            Address[] froms = getMimeMessage().getFrom();
            if (froms != null && froms.length > 0 && froms[0] instanceof InternetAddress) {
                return ((InternetAddress) froms[0]).getAddress();
            }
            Address sender = getMimeMessage().getSender();
            if (sender instanceof InternetAddress) {
                return ((InternetAddress) sender).getAddress();
            }
        } else {
            // Sender header first, then From
            Address sender = getMimeMessage().getSender();
            if (sender instanceof InternetAddress) {
                return ((InternetAddress) sender).getAddress();
            }
            Address[] froms = getMimeMessage().getFrom();
            if (froms != null && froms.length > 0 && froms[0] instanceof InternetAddress) {
                return ((InternetAddress) froms[0]).getAddress();
            }
        }
    } catch (MessagingException e) {
    }
    return null;
}
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) MessagingException(javax.mail.MessagingException)

Aggregations

InternetAddress (javax.mail.internet.InternetAddress)255 MimeMessage (javax.mail.internet.MimeMessage)106 MessagingException (javax.mail.MessagingException)69 Session (javax.mail.Session)49 Properties (java.util.Properties)45 ArrayList (java.util.ArrayList)42 Address (javax.mail.Address)41 Message (javax.mail.Message)40 Date (java.util.Date)38 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)36 AddressException (javax.mail.internet.AddressException)34 X509Certificate (java.security.cert.X509Certificate)32 MimeBodyPart (javax.mail.internet.MimeBodyPart)30 Test (org.junit.Test)29 IOException (java.io.IOException)26 MimeMultipart (javax.mail.internet.MimeMultipart)26 PolicyExpression (org.nhindirect.policy.PolicyExpression)18 HashMap (java.util.HashMap)17 CertificateResolver (org.nhindirect.stagent.cert.CertificateResolver)17 PolicyResolver (org.nhindirect.stagent.policy.PolicyResolver)17