I need to send email through an (external) SMTP server from Java however this server will only accept CRAM-MD5 authentication, which is not supported by JavaMail.
What would be a good way to get these emails to send? (It must be in Java.)
|
|
|
Here is thread which says that you need to add the following property:
Also in Geronimo implementation there is CramMD5Authenticator Hope it helps to resolve this old question. |
|||||
|
|
Since Java Mail 1.4.4, CRAM-MD5 is supported for use with smtp. Just set this parameter to your properties and it will work:
|
|||||||||
|
Very Simple CRAMMD5 program in JAVA
|
||||
|
|
|
Change:
To:
|
|||
|
|
|
I tried the code on the example of a real CRAM-MD5 transaction, and also on the example given into the RFC 2195. It does not work because the conversion to an hexadecimal string is not correct. For exmaple, with this code, you will obtain "b913a62c7eda7a495b4e6e7334d3890" instead of "b913a602c7eda7a495b4e6e7334d3890" and the sent authentication string will not be correct. If you download the source code of javaMail, you will see the implementation of the function toHex into the unit "DigestMD5". Using this conversion, it will work. |
|||
|
|
|
This probably won't help you but CRAM-MD5 and CRAM-SHA1 are fairly easy to implement assuming you have the correct library (md5/sha1) and, idealy, a base64 encoding library (though the base64 stuff is fairly easy to implement yourself in a pinch). The transaction looks like this:
Where NONCE is the once time challenge string, USERNAME is the username you are tryng to authenticate, SECRET is the shared secret ("password"), opad is 0x5C, and ipad is 0x36. (CRAM-SHA1 would be the same transaction, but using SHA1() instead of MD5() to do the digesting) So, here's an example of a real CRAM-MD5 transaction
Backing up the process a step you get:
Backing up one step further to before the digest is calculated, you get:
I guess that's kind of confusing now that I write it out, but trust me, compared to trying to do NTLM/SPA by hand, it's a breeze. If you're motivated, it's actually pretty easy to implement. Or maybe I've just spent way to long with my hands in the guts of mail clients and servers to think about it clearly anymore... |
|||||||||||||
|
|
This doesn't help you directly, however, IMAP connections in JavaMail do support SASL (and thus CRAM-MD5, see the Java SASL documentation) if you set the Unfortunately, there is no However, you can download the JavaMail source code, and you can try to edit the SMTP code to support SASL in a similar manner to the IMAP code. Good luck! |
|||||
|