Java SMTP Authentication only when required
How do we use java mail API so that SMTP authentication is done only when needed. For example, if mail.smtp.auth property is set to true, authentication is done even when it is not required by the SMTP server.
Do I need to first send mail without password, catch exception 开发者_运维问答and based on exception send mail with password?
if mail.smtp.auth property is set to true, authentication is done even when it is not required by the SMTP server.
It is rather hard to believe in the existence or utility of an SMTP server that doesn't require authentication.
Based on this:
I don't have control implied that it is upto the user who use the software.
The simple solution is to allow the user to turn on / off the use of the configured authentication details. There are a number of them, including some that enable / disable authentication.
Beyond that, you are out of luck. You will need to implement your own SMTP provider behavior, either by creating a subclass, or by starting from scratch. Alternatively, catch the exception and retry.
Your particular mode of operation is simply not supported.
For what it is worth, I think that the javamail implementors did the right thing in not supporting this use-case, whether or not they did it deliberately. It would encourage mail server implementors to be sloppy about security, and end users to play silly games with email spoofing.
精彩评论