How to change JavaMail port

后端 未结 4 773
醉话见心
醉话见心 2020-12-18 19:57

I\'m writing a small Java app using JavaMail that sends the user an automated email. They can choose between (for now) two ports: 25 and 587. The port can be selected via

4条回答
  •  醉梦人生
    2020-12-18 20:27

    This happens because you're using getDefaultInstance() which says:

    Get the default Session object. If a default has not yet been setup, a new Session object is created and installed as the default.

    And that the Properties argument is "used only if a new Session object is created."

    So the first time you invoke getDefaultInstance it uses your specified port. After that, the Session has already been created, and subsequent calls to getDefaultInstance will return that same session, and ignore the changed properties.

    Try using Session.getInstance() instead of getDefaultInstance(), which creates a new Session each time, using the supplied properties.

    It pays to read the javadocs very carefully.

提交回复
热议问题