{"id":1354,"date":"2018-03-03T20:51:30","date_gmt":"2018-03-03T18:51:30","guid":{"rendered":"https:\/\/www.cs.ubbcluj.ro\/~bufny\/?p=1354"},"modified":"2018-03-12T13:25:52","modified_gmt":"2018-03-12T11:25:52","slug":"user-authentication-in-java-using-the-imap-or-smtp-protocols","status":"publish","type":"post","link":"https:\/\/www.cs.ubbcluj.ro\/~bufny\/user-authentication-in-java-using-the-imap-or-smtp-protocols\/","title":{"rendered":"User authentication in Java using the IMAP or SMTP protocols"},"content":{"rendered":"<p>Recently, a couple of students have asked me the same question:<\/p>\n<p>&#8220;I need to authenticate users (other students) in my diploma thesis application with their faculty credentials. Is there a way to do this?&#8221;<\/p>\n<p>The faculty student server (i.e. www.scs.ubbcluj.ro) offers two public services: IMAP (on TCP port 993) and SMTP (on TCP port 465). You can use either of these two services to authenticate a user. Just be sure you have the <a href=\"http:\/\/www.oracle.com\/technetwork\/java\/javamail\/\" target=\"_blank\" rel=\"noopener nofollow\">JavaMail<\/a> jar in your classpath. Since April 2017 the JavaMail API is available as an open source project on <a href=\"https:\/\/javaee.github.io\/javamail\/\" target=\"_blank\" rel=\"noopener nofollow\">GitHub<\/a>.<\/p>\n<p>Here is a sample piece of code, just pass the username and the password as command line parameters.<\/p>\n<pre class=\"lang:java nums:false\">import java.util.Properties;\r\nimport javax.mail.internet.*;\r\nimport javax.mail.*;\r\n\r\npublic class JavaMailAuthentication {\r\n\r\n    public static void main(String[] args) throws Exception {\r\n\r\n        String host = \"example.com\"; \/\/ replace example.com with your own host\r\n        Properties props = System.getProperties();\r\n        props.put(\"mail.store.protocol\", \"imaps\");\r\n        props.put(\"mail.imaps.port\", 993);\r\n        props.put(\"mail.imaps.host\", host);\r\n\r\n        javax.mail.Session mailSession = javax.mail.Session.getInstance(props, null);\r\n        Store store = mailSession.getStore();\r\n        try {\r\n            store.connect(host, 993, args[0], args[1]);\r\n            System.out.println(\"Password is ok\");\r\n        } catch (Exception e) {\r\n            System.out.println(\"Password is not ok\");\r\n        }\r\n    }\r\n\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recently, a couple of students have asked me the same question: &#8220;I need to authenticate users (other students) in my diploma thesis application with their faculty credentials. Is there a way to do this?&#8221; The faculty student server (i.e. www.scs.ubbcluj.ro)&hellip; <a href=\"https:\/\/www.cs.ubbcluj.ro\/~bufny\/user-authentication-in-java-using-the-imap-or-smtp-protocols\/\" class=\"more-link\">Continue Reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[130],"tags":[],"_links":{"self":[{"href":"https:\/\/www.cs.ubbcluj.ro\/~bufny\/wp-json\/wp\/v2\/posts\/1354"}],"collection":[{"href":"https:\/\/www.cs.ubbcluj.ro\/~bufny\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cs.ubbcluj.ro\/~bufny\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cs.ubbcluj.ro\/~bufny\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cs.ubbcluj.ro\/~bufny\/wp-json\/wp\/v2\/comments?post=1354"}],"version-history":[{"count":7,"href":"https:\/\/www.cs.ubbcluj.ro\/~bufny\/wp-json\/wp\/v2\/posts\/1354\/revisions"}],"predecessor-version":[{"id":1408,"href":"https:\/\/www.cs.ubbcluj.ro\/~bufny\/wp-json\/wp\/v2\/posts\/1354\/revisions\/1408"}],"wp:attachment":[{"href":"https:\/\/www.cs.ubbcluj.ro\/~bufny\/wp-json\/wp\/v2\/media?parent=1354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cs.ubbcluj.ro\/~bufny\/wp-json\/wp\/v2\/categories?post=1354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cs.ubbcluj.ro\/~bufny\/wp-json\/wp\/v2\/tags?post=1354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}