How to change JavaMail port

后端 未结 4 774
醉话见心
醉话见心 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:09

    Tip for anyone else still having issues, we were using Session.getInstance and the port was still defaulting to 25.

    Turns out, we were setting the prop value as a Long when it needs to be a String

    It didn't error, warn or log, just defaulted to 25.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-18 20:32

    I think "Transport.send(msg)" wont be taking into account the connection details that you are providing in your properties. It will use its connection that is defined by default.

    The java doc says

    "Note that send is a static method that creates and manages its own connection. **Any connection associated with any Transport instance used to invoke this method is ignored and not used. This method should only be invoked using the form Transport.send(msg);, and should never be invoked using an instance variable. "**

    Instead, I have tried with Transport.connect(smtphost,smtpport,user,password) and it works pretty well.

    0 讨论(0)
  • 2020-12-18 20:32

    Plz compare two methods of Session class: Session.getDefaultInstance(Properties, Authenticator) and Session.getInstance(Properties, Authenticator)

    0 讨论(0)
提交回复
热议问题