【问题标题】:Validate smtp server credentials using java without actually sending mail使用 java 验证 smtp 服务器凭据而不实际发送邮件
【发布时间】:2011-03-04 21:37:57
【问题描述】:

要验证 smtp 服务器凭据,我应该使用transport.connect()吗?

Session session = Session.getInstance(properties,authenticator);

 Transport tr=session.getTransport("smtp");

 tr.connect();

检查 smtp 服务器凭据的方法是否正确?

【问题讨论】:

    标签: java jakarta-mail smtp-auth


    【解决方案1】:

    这个问题:'Verify mail server connection programmatically in ColdFusion' 有一个 java 解决方案作为已接受答案的一部分:

    int port = 587;
    String host = "smtp.gmail.com";
    String user = "username@gmail.com";
    String pwd = "email password";
    
    try {
        Properties props = new Properties();
        // required for gmail 
        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.auth", "true");
        // or use getDefaultInstance instance if desired...
        Session session = Session.getInstance(props, null);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, port, user, pwd);
        transport.close();
        System.out.println("success");
     } 
     catch(AuthenticationFailedException e) {
           System.out.println("AuthenticationFailedException - for authentication failures");
           e.printStackTrace();
     }
     catch(MessagingException e) {
           System.out.println("for other failures");
           e.printStackTrace();
     }
    

    【讨论】:

    • 我已经尝试过了,但它不适用于 gmail 以外的地址 - 总是打印成功。知道为什么吗?我正在使用 smtp.1and1.com 并且不正确的凭据仍然打印成功
    • 我尝试了您的解决方案,但即使数据正确(我现在正在使用这些电子邮件数据设置发送电子邮件)我得到:535 身份验证数据不正确知道为什么吗?
    【解决方案2】:
    public boolean confirmSMTP(String host, String port, String username, String password, String auth, String enctype) {
        boolean result = false;
        try {
            Properties props = new Properties();
            if (auth.equals(true)) {
                props.setProperty("mail.smtp.auth", "true"); 
            } else { 
                props.setProperty("mail.smtp.auth", "false"); 
            }
            if (enctype.endsWith("TLS")) {
                props.setProperty("mail.smtp.starttls.enable", "true");
            } else if (enctype.endsWith("SSL")) {
                props.setProperty("mail.smtp.startssl.enable", "true");
            }
            Session session = Session.getInstance(props, null);
            Transport transport = session.getTransport("smtp");
            int portint = Integer.parseInt(port);
            transport.connect(host, portint, username, password);
            transport.close();
            result = true;
    
        } catch(AuthenticationFailedException e) {
            Logging.addMsg(e.toString(), "SMTP: Authentication Failed", false, true);
    
        } catch(MessagingException e) {
            Logging.addMsg(e.toString(), "SMTP: Messaging Exception Occurred", false, true);
        } catch (Exception e) {
            Logging.addMsg(e.toString(), "SMTP: Unknown Exception", false, true);
        }
    
        return result;
    }
    

    【讨论】:

    • Bak,我试过这个,但就像上面的答案一样,我不应该收到成功消息。我正在做的事情可能有问题吗?
    猜你喜欢
    • 2011-02-10
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多