使用java验证smtp服务器凭据而不实际发送邮件

发布于 09-06 15:55 字数 245 浏览 8 评论 0原文

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

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

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

 tr.connect();

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

To verify smtp server credentials shall I use transport.connect()?

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

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

 tr.connect();

Is it correct method to check smtp server credentials?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风向决定发型2024-09-13 15:55:37

这个问题:“在 ColdFusion 中以编程方式验证邮件服务器连接” 有一个java解决方案作为接受答案的一部分:

int port = 587;
String host = "smtp.gmail.com";
String user = "[email protected]";
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();
 }

This question: 'Verify mail server connection programmatically in ColdFusion' has a java solution as part of the accepted answer:

int port = 587;
String host = "smtp.gmail.com";
String user = "[email protected]";
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();
 }
回忆追雨的时光2024-09-13 15:55:37
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文