// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;

public class BasicAuthEncode {
    public static String basicauth(String un, String  pw) throws MessagingException, IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream b64os = MimeUtility.encode(baos, "base64");
        b64os.write((un + ":" + pw).getBytes());
        b64os.close();
        return new String(baos.toByteArray());
     }
    public static void main(String[] args) throws Exception {
        System.out.println(basicauth("ciao","lr"));
    }
}
