Search in sources :

Example 31 with JavaMailInternetAddress

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

the class Mime method parseAddressHeader.

public static InternetAddress[] parseAddressHeader(String header, boolean expandGroups) {
    if (header == null || header.trim().isEmpty())
        return NO_ADDRESSES;
    header = header.trim();
    InternetAddress[] addresses;
    try {
        addresses = JavaMailInternetAddress.parseHeader(header, false);
    } catch (Throwable e) {
        // Catch everything in case MIME parser was not robust enough to handle a malformed header.
        if (e instanceof OutOfMemoryError) {
            Zimbra.halt("MIME parser failed: " + header, e);
        } else if (!(e instanceof AddressException)) {
            sLog.error("MIME parser failed: " + header, e);
        }
        try {
            return new InternetAddress[] { new JavaMailInternetAddress(null, header, MimeConstants.P_CHARSET_UTF8) };
        } catch (UnsupportedEncodingException e1) {
            return NO_ADDRESSES;
        }
    }
    if (!expandGroups)
        return addresses;
    boolean hasGroups = false;
    for (InternetAddress addr : addresses) {
        if (addr.isGroup()) {
            hasGroups = true;
            break;
        }
    }
    if (!hasGroups)
        return addresses;
    // if we're here, we need to expand at least one group...
    List<InternetAddress> expanded = new ArrayList<InternetAddress>();
    for (InternetAddress addr : addresses) {
        if (!addr.isGroup()) {
            expanded.add(addr);
        } else {
            try {
                InternetAddress[] members = addr.getGroup(false);
                if (members == null)
                    expanded.add(addr);
                else
                    for (InternetAddress member : members) expanded.add(member);
            } catch (AddressException e) {
                expanded.add(addr);
            }
        }
    }
    return expanded.toArray(new InternetAddress[expanded.size()]);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) AddressException(javax.mail.internet.AddressException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 32 with JavaMailInternetAddress

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

the class AccountUtil method getFriendlyEmailAddress.

public static InternetAddress getFriendlyEmailAddress(Account acct) {
    // check "displayName" for personal part, and fall back to "cn" if not present
    String personalPart = acct.getAttr(Provisioning.A_displayName);
    if (personalPart == null)
        personalPart = acct.getAttr(Provisioning.A_cn);
    // catch the case where no real name was present and so cn was defaulted to the username
    if (personalPart == null || personalPart.trim().equals("") || personalPart.equals(acct.getAttr("uid")))
        personalPart = null;
    String address;
    try {
        address = getCanonicalAddress(acct);
    } catch (ServiceException se) {
        ZimbraLog.misc.warn("unexpected exception canonicalizing address, will use account name", se);
        address = acct.getName();
    }
    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) ServiceException(com.zimbra.common.service.ServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 33 with JavaMailInternetAddress

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

the class SmtpInject method main.

public static void main(String[] args) {
    CliUtil.toolSetup();
    CommandLine cl = parseArgs(args);
    if (cl.hasOption("h")) {
        usage(null);
    }
    String file = null;
    if (!cl.hasOption("f")) {
        usage("no file specified");
    } else {
        file = cl.getOptionValue("f");
    }
    try {
        ByteUtil.getContent(new File(file));
    } catch (IOException ioe) {
        usage(ioe.getMessage());
    }
    String host = null;
    if (!cl.hasOption("a")) {
        usage("no smtp server specified");
    } else {
        host = cl.getOptionValue("a");
    }
    String sender = null;
    if (!cl.hasOption("s")) {
        usage("no sender specified");
    } else {
        sender = cl.getOptionValue("s");
    }
    String recipient = null;
    if (!cl.hasOption("r")) {
        usage("no recipient specified");
    } else {
        recipient = cl.getOptionValue("r");
    }
    boolean trace = false;
    if (cl.hasOption("T")) {
        trace = true;
    }
    boolean tls = false;
    if (cl.hasOption("t")) {
        tls = true;
    }
    boolean auth = false;
    String user = null;
    String password = null;
    if (cl.hasOption("A")) {
        auth = true;
        if (!cl.hasOption("u")) {
            usage("auth enabled, no user specified");
        } else {
            user = cl.getOptionValue("u");
        }
        if (!cl.hasOption("p")) {
            usage("auth enabled, no password specified");
        } else {
            password = cl.getOptionValue("p");
        }
    }
    if (cl.hasOption("v")) {
        mLog.info("SMTP server: " + host);
        mLog.info("Sender: " + sender);
        mLog.info("Recipient: " + recipient);
        mLog.info("File: " + file);
        mLog.info("TLS: " + tls);
        mLog.info("Auth: " + auth);
        if (auth) {
            mLog.info("User: " + user);
            char[] dummyPassword = new char[password.length()];
            Arrays.fill(dummyPassword, '*');
            mLog.info("Password: " + new String(dummyPassword));
        }
    }
    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);
    if (auth) {
        props.put("mail.smtp.auth", "true");
    } else {
        props.put("mail.smtp.auth", "false");
    }
    if (tls) {
        props.put("mail.smtp.starttls.enable", "true");
    } else {
        props.put("mail.smtp.starttls.enable", "false");
    }
    // Disable certificate checking so we can test against
    // self-signed certificates
    props.put("mail.smtp.ssl.socketFactory", SocketFactories.dummySSLSocketFactory());
    Session session = Session.getInstance(props, null);
    session.setDebug(trace);
    try {
        // create a message
        MimeMessage msg = new ZMimeMessage(session, new ZSharedFileInputStream(file));
        InternetAddress[] address = { new JavaMailInternetAddress(recipient) };
        msg.setFrom(new JavaMailInternetAddress(sender));
        // attach the file to the message
        Transport transport = session.getTransport("smtp");
        transport.connect(null, user, password);
        transport.sendMessage(msg, address);
    } catch (MessagingException mex) {
        mex.printStackTrace();
        Exception ex = null;
        if ((ex = mex.getNextException()) != null) {
            ex.printStackTrace();
        }
        System.exit(1);
    }
}
Also used : JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) InternetAddress(javax.mail.internet.InternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Properties(java.util.Properties) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) ZSharedFileInputStream(com.zimbra.common.zmime.ZSharedFileInputStream) CommandLine(org.apache.commons.cli.CommandLine) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Transport(javax.mail.Transport) File(java.io.File) Session(javax.mail.Session)

Example 34 with JavaMailInternetAddress

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

the class AccountUtil method getReplyToAddress.

/**
 * Returns the <tt>Reply-To</tt> address used for an outgoing message from the given
 * account, based on user preferences, or <tt>null</tt> if <tt>zimbraPrefReplyToEnabled</tt>
 * is <tt>FALSE</tt>.
 */
public static InternetAddress getReplyToAddress(Account acct) {
    if (acct == null) {
        return null;
    }
    if (!acct.isPrefReplyToEnabled()) {
        return null;
    }
    String address = acct.getPrefReplyToAddress();
    if (address == null) {
        return null;
    }
    String personal = acct.getPrefReplyToDisplay();
    try {
        return new JavaMailInternetAddress(address, personal, MimeConstants.P_CHARSET_UTF8);
    } catch (UnsupportedEncodingException e) {
        ZimbraLog.system.error("Unable to encode address %s <%s>", personal, address);
        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 35 with JavaMailInternetAddress

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

the class AccountUtil method getFromAddress.

/**
 * Returns the <tt>From</tt> address used for an outgoing message from the given account.
 * Takes all account attributes into consideration, including user preferences.
 */
public static InternetAddress getFromAddress(Account acct) {
    if (acct == null) {
        return null;
    }
    String address = SystemUtil.coalesce(acct.getPrefFromAddress(), acct.getName());
    String personal = SystemUtil.coalesce(acct.getPrefFromDisplay(), acct.getDisplayName(), acct.getCn());
    try {
        return new JavaMailInternetAddress(address, personal, MimeConstants.P_CHARSET_UTF8);
    } catch (UnsupportedEncodingException e) {
        ZimbraLog.system.error("Unable to encode address %s <%s>", personal, address);
        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)

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