SMTP with CRAM-MD5 in Java

I need to send email through a (external) SMTP server from Java, but this server will only accept CRAM-MD5 authentication, which is not supported by JavaMail.

What would be a good way to send these letters? (This should be in Java.)

+5
source share
7 answers

Here is a thread that says you need to add the following property:

props.put("mail.smtp.auth.mechanisms", "CRAM-MD5")

Also in the Geronimo implementation there is CramMD5Authenticator

Hope this helps resolve this old question.

+6
source

, , , CRAM-MD5 CRAM-SHA1 , (md5/sha1) , , base64 ( base64 ).

:

C: AUTH CRAM-MD5
S: 334 BASE64(NONCE)
C: BASE64(USERNAME, " ", MD5((SECRET XOR opad),MD5((SECRET XOR ipad), NONCE)))
S: 235 Authentication succeeded

NONCE - , USERNAME - , , SECRET - ( "" ), opad - 0x5C, ipad - 0x36.

(CRAM-SHA1 , SHA1() MD5(), )

, CRAM-MD5

C: AUTH CRAM-MD5
S: 334 PDQ1MDMuMTIyMzU1Nzg2MkBtYWlsMDEuZXhhbXBsZS5jb20+
C: dXNlckBleGFtcGxlLmNvbSA4YjdjODA5YzQ0NTNjZTVhYTA5N2VhNWM4OTlmNGY4Nw==
S: 235 Authentication succeeded

:

S: 334 BASE64("<4503.1223557862@mail01.example.com>")
C: BASE64("user@example.com 8b7c809c4453ce5aa097ea5c899f4f87")

:

S: 334 BASE64("<4503.1223557862@mail01.example.com>")
C: BASE64("user@example.com ", MD5(("password" XOR opad),MD5(("password" XOR ipad), "<4503.1223557862@mail01.example.com>")))

, , , , NTLM/SPA , . , . , , , ...

+4

CRAMMD5 JAVA


import java.security.*;

class CRAMMD5Test
{
public static void main(String[] args) throws Exception
{
    // This represents the BASE64 encoded timestamp sent by the POP server
    String dataString = Base64Decoder.decode("PDAwMDAuMDAwMDAwMDAwMEBteDEuc2VydmVyLmNvbT4=");
    byte[] data = dataString.getBytes();

    // The password to access the account
    byte[] key  = new String("password").getBytes();

    // The address of the e-mail account
    String user = "client@server.com";

    MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.reset();

    if (key.length > 64)
        key = md5.digest(key);

    byte[] k_ipad = new byte[64];
    byte[] k_opad = new byte[64];

    System.arraycopy(key, 0, k_ipad, 0, key.length);
    System.arraycopy(key, 0, k_opad, 0, key.length);

    for (int i=0; i<64; i++)
    {
        k_ipad[i] ^= 0x36;
        k_opad[i] ^= 0x5c;
    }

    byte[] i_temp = new byte[k_ipad.length + data.length];

    System.arraycopy(k_ipad, 0, i_temp, 0, k_ipad.length);
    System.arraycopy(data, 0, i_temp, k_ipad.length, data.length);

    i_temp = md5.digest(i_temp);

    byte[] o_temp = new byte[k_opad.length + i_temp.length];

    System.arraycopy(k_opad, 0, o_temp, 0, k_opad.length);
    System.arraycopy(i_temp, 0, o_temp, k_opad.length, i_temp.length);

        byte[] result = md5.digest(o_temp);
        StringBuffer hexString = new StringBuffer();

        for (int i=0;i < result.length; i++) {
                hexString.append(Integer.toHexString((result[i] >>> 4) & 0x0F));
                hexString.append(Integer.toHexString(0x0F & result[i]));
             }


        System.out.println(Base64Encoder.encode(user + " " + hexString.toString()));
    }
}
+4

, IMAP JavaMail SASL (, , CRAM-MD5, . Java SASL), mail.imap.sasl.enable boolean true.

, mail.smtp.sasl.enable, SASL SMTP JavaMail.: - (

JavaMail, SMTP- SASL IMAP. !

+3

Java Mail 1.4.4, CRAM-MD5 smtp. , :

props.put("mail.smtp.sasl.enable", "true");

+3

CRAM-MD5, , RFC 2195.

, . , , "b913a62c7eda7a495b4e6e7334d3890" "b913a602c7eda7a495b4e6e7334d3890", .

If you download the javaMail source code, you will see the implementation of the toHex function in the "DigestMD5" module. Using this conversion, it will work.

+2
source

Change:

for (int i=0; i<result.length; i++)
  hexString.append(Integer.toHexString(0xFF & result[i]));

To:

for (int i=0;i < result.length; i++) {
  hexString.append(Integer.toHexString((result[i] >>> 4) & 0x0F));
  hexString.append(Integer.toHexString(0x0F & result[i]));
}
+1
source

All Articles