javamail身份验证器不起作用

时间:2017-04-25 16:35:14

标签: java smtp javamail gssapi authenticator

这是我的配置,用于向我的smtp服务器发送邮件 但它不使用会话验证器并强制我从控制台输入凭证,当我从控制台输入凭据时,它成功发送邮件,我的配置部分错误。

        Properties properties = new Properties();
    //clear it later 

    properties.setProperty("mail.debug", "true");
    properties.put("mail.smtp.socketFactory.port", "587");
    properties.put("mail.smtp.auth", true);
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", "mail.example.com");
    properties.put("mail.smtp.port", "587");

    properties.put("mail.smtp.host", "mail.example.com");
    properties.put("mail.smtp.sasl.enable","true");   
    properties.put("mail.smtp.sasl.mechanisms","GSSAPI");
    properties.put("mail.smtp.sasl.authorizationid","admin");
    properties.put("mail.smtp.sasl.realm","EXAMPLE.COM");

    System.setProperty( "sun.security.krb5.debug", "true");
    System.setProperty( "java.security.krb5.realm", "EXAMPLE.COM");
    System.setProperty( "java.security.krb5.kdc", "ipa.example.com"); 


    URL jaasConfigURL = this.getClass().getResource("jaas.conf");
    jaasConfigURL.getFile();
    String jaasConfigFile = jaasConfigURL.getFile();
    System.out.println(jaasConfigFile);
    System.setProperty( "java.security.auth.login.config", jaasConfigFile);
    System.setProperty( "javax.security.auth.useSubjectCredsOnly", "false");

    session = Session.getInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication(){
            return new PasswordAuthentication("admin", "password");
        }
     });
        session.setDebug(true);
    MimeMessage mime = new SMTPMessage(session);
    mime.setFrom(new InternetAddress(from));
    mime.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
    mime.setSubject("test");
    mime.setText("hello world!");
    Transport.send(mime);

和我的jaas.conf文件

krb5_initiate{
com.sun.security.auth.module.krb5LoginModule required
useTicketCache="true"
doNotPrompt="true"
debug="true";
};

0 个答案:

没有答案