Search in sources :

Example 81 with InternetAddress

use of javax.mail.internet.InternetAddress 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: 中文
             *      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 82 with InternetAddress

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

the class IDNUtil method toUnicodeWithPersonalPart.

private static String toUnicodeWithPersonalPart(String name) {
    try {
        InternetAddress ia = new JavaMailInternetAddress(name, true);
        /*
             * InternetAddress.toUnicodeString only deals with 
             * non-ascii chars in personal part, it has nothing 
             * to do with IDN.
             */
        // return ia.toUnicodeString();   
        String addr = ia.getAddress();
        String unicodeAddr = toUnicode(addr);
        try {
            ia = new JavaMailInternetAddress(unicodeAddr, ia.getPersonal(), MimeConstants.P_CHARSET_UTF8);
            /*
                 *  call InternetAddress.toUnicodeString instead of 
                 *  InternetAddress.toString so non-ascii chars in 
                 *  personal part can be returned in Unicode instead of 
                 *  RFC 2047 encoded.
                 */
            return ia.toUnicodeString();
        } catch (UnsupportedEncodingException e) {
            ZimbraLog.account.info("cannot convert to unicode, returning original addr: [" + name + "]", e);
        }
    } catch (AddressException e) {
        ZimbraLog.account.info("cannot convert to unicode, returning original addr: [" + name + "]", e);
    }
    return name;
}
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) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 83 with InternetAddress

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

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

use of javax.mail.internet.InternetAddress in project perun by CESNET.

the class MessagePreparator method prepare.

/*
	 * (non-Javadoc)
	 *
	 * @see org.springframework.mail.javamail.MimeMessagePreparator#prepare(javax.mail.internet.MimeMessage)
	 */
public void prepare(MimeMessage mimeMessage) throws Exception {
    MimeMultipart mpRoot = new MimeMultipart("mixed");
    Multipart mp = new MimeMultipart("alternative");
    // Create a body part to house the multipart/alternative Part
    MimeBodyPart contentPartRoot = new MimeBodyPart();
    contentPartRoot.setContent(mp);
    // Add the root body part to the root multipart
    mpRoot.addBodyPart(contentPartRoot);
    // adding recipients, cc and bcc
    if (getTo() != null) {
        for (String to : getTo()) {
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        }
    }
    if (getCc() != null) {
        for (String cc : getCc()) {
            mimeMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
        }
    }
    if (getBcc() != null) {
        for (String bcc : getBcc()) {
            mimeMessage.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
        }
    }
    // adding from and subject
    mimeMessage.setFrom(new InternetAddress(getFrom(), getFromText()));
    mimeMessage.setSubject(getSubject());
    if (getEmailType().equals(EmailType.HTML)) {
        mp.addBodyPart(createHtmlMessage());
    } else if (getEmailType().equals(EmailType.PLAIN)) {
        mp.addBodyPart(createTextMessage());
    }
    // Create an "ATTACHMENT" - we must put it to mpRoot(mixed content)
    if (getFileNames() != null) {
        for (String filename : getFileNames()) {
            mpRoot.addBodyPart(createAttachment(filename));
        }
    }
    mimeMessage.setContent(mpRoot);
    logger.debug("Message is prepared to send");
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

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