JavaMail(smtps& imaps)

时间:2011-04-01 15:23:16

标签: java javamail

如何配置JavaMail以使用smtps和imaps?

1 个答案:

答案 0 :(得分:2)

这是设置SMTPS

private static Session createSession(String host, int port, boolean sslAuthenticationRequired, String userName, String password, boolean debug) {
        Properties properties = new Properties();
        String mailSmtp = "mail.smtp";

        if (sslAuthenticationRequired) {
            mailSmtp += "s";

            properties.put(mailSmtp + ".socketFactory.port", Integer.toString(port));
            properties.put(mailSmtp + ".socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            properties.put(mailSmtp + ".socketFactory.fallback", "false");
        }

        properties.put("mail.debug", debug ? "true" : "false");     
        properties.put(mailSmtp + ".host", host);
        properties.put(mailSmtp + ".user", userName);
        properties.put(mailSmtp + ".password", password);
        properties.put(mailSmtp + ".port", Integer.toString(port));
        properties.put(mailSmtp + ".starttls.enable", "true");
        properties.put(mailSmtp + ".auth", "true");

        return Session.getDefaultInstance(properties, new PasswordAuthenticator(userName, password));
    }

this link解释了如何配置IMAP。