Search in sources :

Example 11 with JavaMailInternetAddress

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

the class IDNUtil method toAsciiWithPersonalPart.

private static String toAsciiWithPersonalPart(String name) {
    String asciiName = name;
    // extract out the domain, convert it to ACE, and put it back
    Matcher m = S_DOMAIN.matcher(name);
    if (m.matches() && m.groupCount() == 4) {
        // if matches(), then groupCount() must be 4 according to our regex, just to be safe
        String domain = m.group(3);
        String asciiDomain = toAsciiDomainName(domain);
        // put everything back
        asciiName = m.group(1) + m.group(2) + asciiDomain + m.group(4);
    }
    try {
        InternetAddress ia = new JavaMailInternetAddress(asciiName);
        String personal = ia.getPersonal();
        if (personal != null)
            ia = new JavaMailInternetAddress(ia.getAddress(), personal, MimeConstants.P_CHARSET_UTF8);
        /*
             * note, if personal part contains non-ascii chars, it will be 
             * converted (by InternetAddress.toString()) to RFC 2047 encoded address.
             * The resulting string contains only US-ASCII characters, and
             * hence is mail-safe.
             * 
             * e.g. a personal part: \u4e2d\u6587
             *      will be converted to =?utf-8?B?5Lit5paH?=
             *      and stored in LDAP in the encoded form
             */
        return ia.toString();
    } catch (UnsupportedEncodingException e) {
        ZimbraLog.account.info("cannot convert to ascii, returning original addr: [" + name + "]", e);
    } catch (AddressException e) {
        ZimbraLog.account.info("cannot convert to ascii, returning original addr: [" + name + "]", e);
    }
    return name;
}
Also used : JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) InternetAddress(javax.mail.internet.InternetAddress) Matcher(java.util.regex.Matcher) AddressException(javax.mail.internet.AddressException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 12 with JavaMailInternetAddress

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

the class Identity method getFriendlyEmailAddress.

public InternetAddress getFriendlyEmailAddress() {
    String personalPart = getAttr(Provisioning.A_zimbraPrefFromDisplay);
    if (personalPart == null || personalPart.trim().equals(""))
        personalPart = null;
    String address = getAttr(Provisioning.A_zimbraPrefFromAddress);
    try {
        return new JavaMailInternetAddress(address, personalPart, MimeConstants.P_CHARSET_UTF8);
    } catch (UnsupportedEncodingException e) {
    }
    // UTF-8 should *always* be supported (i.e. this is actually unreachable)
    try {
        // fall back to using the system's default charset (also pretty much guaranteed not to be "unsupported")
        return new JavaMailInternetAddress(address, personalPart);
    } catch (UnsupportedEncodingException e) {
    }
    // if we ever reached this point (which we won't), just return an address with no personal part
    InternetAddress ia = new JavaMailInternetAddress();
    ia.setAddress(address);
    return ia;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 13 with JavaMailInternetAddress

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

the class AttributeInfo method validEmailAddress.

public static void validEmailAddress(String addr, boolean personal) throws ServiceException {
    if (addr.indexOf('@') == -1)
        throw AccountServiceException.INVALID_ATTR_VALUE("address '" + addr + "' does not include domain", null);
    try {
        InternetAddress ia = new JavaMailInternetAddress(addr, true);
        // is this even needed?
        ia.validate();
        if (!personal && ia.getPersonal() != null && !ia.getPersonal().equals(""))
            throw AccountServiceException.INVALID_ATTR_VALUE("invalid email address: " + addr, null);
    } catch (AddressException e) {
        throw AccountServiceException.INVALID_ATTR_VALUE("invalid email address: " + addr, e);
    }
}
Also used : JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress)

Example 14 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress 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 = AccountUtil.getSmtpMessageObj(acct);
        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 15 with JavaMailInternetAddress

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

the class CalendarMailSender method createOrganizerChangeMessage.

public static MimeMessage createOrganizerChangeMessage(Account fromAccount, Account authAccount, boolean asAdmin, CalendarItem calItem, Invite inv, List<Address> rcpts) throws ServiceException {
    ZOrganizer organizer = inv.getOrganizer();
    assert (organizer != null);
    boolean onBehalfOf = organizer.hasSentBy();
    String senderAddr = onBehalfOf ? organizer.getSentBy() : organizer.getAddress();
    Locale locale = fromAccount.getLocale();
    boolean hidePrivate = !calItem.isPublic() && !calItem.allowPrivateAccess(authAccount, asAdmin);
    String subject;
    if (hidePrivate) {
        subject = L10nUtil.getMessage(MsgKey.calendarSubjectWithheld, locale);
    } else {
        subject = inv.getName();
    }
    StringBuilder sb = new StringBuilder("Organizer has been changed to " + fromAccount.getName());
    sb.append("\r\n\r\n");
    if (!hidePrivate) {
        MimeMessage mmInv = inv.getMimeMessage();
        if (mmInv != null) {
            attachInviteSummary(sb, inv, mmInv, locale);
        }
    }
    ZVCalendar iCal = inv.newToICalendar(true);
    Address from = AccountUtil.getFriendlyEmailAddress(fromAccount);
    Address sender = null;
    if (onBehalfOf) {
        try {
            sender = new JavaMailInternetAddress(senderAddr);
        } catch (AddressException e) {
            throw MailServiceException.ADDRESS_PARSE_ERROR(e);
        }
    }
    return createCalendarMessage(authAccount, from, sender, rcpts, subject, sb.toString(), null, inv.getUid(), iCal);
}
Also used : Locale(java.util.Locale) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) 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