Когда mail.smtp.password стал неработоспособным? - PullRequest
3 голосов
/ 19 октября 2011

У меня есть следующий код для отправки электронной почты:

Properties props = new Properties();
props.put("mail.smtp.host", "host");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", "username");
props.put("mail.smtp.password", "password");

Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);

Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("from@example.com"));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress("to@example.com"));
msg.setSubject("HEY " + new Date());
msg.setContent("This is a test message", "text/plain");
msg.setSentDate(new Date());
Transport transport = session.getTransport("smtp");
transport.connect();
transport.sendMessage(msg, msg.getAllRecipients());

Различные сайты в Интернете указывают на mail.smtp.password, который используется для передачи пароля для аутентификации SMTP Java Mail. Однако, похоже, это не работает с JavaMail 1.4.4 и приведенным выше кодом.

Это что-то, что устарело?

Ответы [ 3 ]

3 голосов
/ 19 октября 2011

(последняя) JavaMail javadoc не упоминает использование mail.smtp.password способа указания пароля, необходимого для аутентификации.Не уверен, что это сработало раньше.Я всегда использовал Authenticator для этой цели.

0 голосов
/ 05 марта 2014

Я не знаю, что это когда-либо было функциональным, и я использую JavaMail около десяти лет.Вероятно, городской миф.Это было бы скорее дырой в безопасности, если бы это действительно работало.Используйте интерфейс аутентификатора.

0 голосов
/ 05 марта 2014

javax.mail.Service выполняет connect() подключение к указанному хосту и порту.Там он использует javax.mail.Authenticator для аутентификации.

Код из javax.mail.Service.connect() метод:

public synchronized void connect(String host, int port,
        String user, String password) throws MessagingException {

    // see if the service is already connected
    if (isConnected())
        throw new IllegalStateException("already connected");

    PasswordAuthentication pw;
    boolean connected = false;
    boolean save = false;
    String protocol = null;
    String file = null;

    // get whatever information we can from the URL
    // XXX - url should always be non-null here, Session
    //       passes it into the constructor
    if (url != null) {
        protocol = url.getProtocol();
        if (host == null)
        host = url.getHost();
        if (port == -1)
        port = url.getPort();

        if (user == null) {
        user = url.getUsername();
        if (password == null)   // get password too if we need it
            password = url.getPassword();
        } else {
        if (password == null && user.equals(url.getUsername()))
            // only get the password if it matches the username
            password = url.getPassword();
        }

        file = url.getFile();
    }

    // try to get protocol-specific default properties
    if (protocol != null) {
        if (host == null)
        host = session.getProperty("mail." + protocol + ".host");
        if (user == null)
        user = session.getProperty("mail." + protocol + ".user");
    }

    // try to get mail-wide default properties
    if (host == null)
        host = session.getProperty("mail.host");

    if (user == null)
        user = session.getProperty("mail.user");

    // try using the system username
    if (user == null) {
        try {
        user = System.getProperty("user.name");
        } catch (SecurityException sex) {
        if (debug)
            sex.printStackTrace(session.getDebugOut());
        }
    }

    // if we don't have a password, look for saved authentication info
    if (password == null && url != null) {
        // canonicalize the URLName
        setURLName(new URLName(protocol, host, port, file, user, null));
        pw = session.getPasswordAuthentication(getURLName());
        if (pw != null) {
        if (user == null) {
            user = pw.getUserName();
            password = pw.getPassword();
        } else if (user.equals(pw.getUserName())) {
            password = pw.getPassword();
        }
        } else
        save = true;
    }

    // try connecting, if the protocol needs some missing
    // information (user, password) it will not connect.
    // if it tries to connect and fails, remember why for later.
    AuthenticationFailedException authEx = null;
    try {
        connected = protocolConnect(host, port, user, password);
    } catch (AuthenticationFailedException ex) {
        authEx = ex;
    }

    // if not connected, ask the user and try again
    if (!connected) {
        InetAddress addr;
        try {
        addr = InetAddress.getByName(host);
        } catch (UnknownHostException e) {
        addr = null;
        }
        pw = session.requestPasswordAuthentication(
                addr, port,
                protocol,
                null, user);
        if (pw != null) {
        user = pw.getUserName();
        password = pw.getPassword();

        // have the service connect again
        connected = protocolConnect(host, port, user, password);
        }
    }

    // if we're not connected by now, we give up
    if (!connected) {
        if (authEx != null)
        throw authEx;
        else if (user == null)
        throw new AuthenticationFailedException(
            "failed to connect, no user name specified?");
        else if (password == null)
        throw new AuthenticationFailedException(
            "failed to connect, no password specified?");
        else
        throw new AuthenticationFailedException("failed to connect");
    }

    setURLName(new URLName(protocol, host, port, file, user, password));

    if (save)
        session.setPasswordAuthentication(getURLName(),
                new PasswordAuthentication(user, password));

    // set our connected state
    setConnected(true);

    // finally, deliver the connection event
    notifyConnectionListeners(ConnectionEvent.OPENED);
    }
...