Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 3.1
    • Fix Version/s: 3.2
    • Component/s: SMTP
    • Labels:
      None

      Description

      It would be nice if commons-net supported XOAUTH for authentication.
      I could modify the code slightly to get this working, but I thought maybe there is a better way.
      If not, please have a look at my patch and see if it is worthwhile adding it to trunk.
      Thanks

        Activity

        Hide
        Houman Atashbar added a comment -

        Here is my patch.

        Show
        Houman Atashbar added a comment - Here is my patch.
        Hide
        Houman Atashbar added a comment -

        This is how you might use it:

        public final class SMTPSMail
        {

        public final static void main(String[] args)
        {
        String sender = "src.email@gmail.com";
        String senderXoauthToken = "R0VUIGh0dHBzOi8vbWFpbC5nb29nbGUuY29tL21haWwvYi9yZWxheW1lLnRpbnl3ZWJnZWFyc0BnbWFpbC5jb20vc210cC8gb2F1dGhfc2lnbmF0dXJlPSJIekpxMFlVdGlwZ3FVTHk3bWVqcCUyRktJYlg3OCUzRCIsc2NvcGU9Imh0dHBzJTNBJTJGJTJGd3d3Lmdvb2dsZWFwaXMuY29tJTJGYXV0aCUyRnVzZXJpbmZvLmVtYWlsJTIwaHR0cHMlM0ElMkYlMkZtYWlsLmdvb2dsZS5jb20lMkYiLG9hdXRoX3ZlcnNpb249IjEuMCIsb2F1dGhfbm9uY2U9IjgzODI2MDUzNyIsb2F1dGhfc2lnbmF0dXJlX21ldGhvZD0iSE1BQy1TSEExIixvYXV0aF9jb25zdW1lcl9rZXk9InJlbGF5bWUudGlueXdlYmdlYXJzLmNvbSIsb2F1dGhfdG9rZW49IjElMkZHVDA0QmxaOWVwYjk1WE10QURSSjNsNEd6b1R4YTJlR3BlYmxvZXRpT0ZvIixvYXV0aF90aW1lc3RhbXA9IjEzNDQ3NDI2MTki";
        String recipient = "destination.email@gmail.com";
        String subject = "Testing Commons Net";
        String body = "Testing at " + new Date();
        String server = "smtp.gmail.com";
        Integer port = 587;

        try
        {
        SimpleSMTPHeader header = new SimpleSMTPHeader(sender, recipient, subject);

        AuthenticatingSMTPClient client = new AuthenticatingSMTPClient();
        client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
        client.connect(server, port);
        client.login();
        client.execTLS();
        client.auth(AuthenticatingSMTPClient.AUTH_METHOD.XOAUTH, senderXoauthToken, null);

        if (!SMTPReply.isPositiveCompletion(client.getReplyCode()))

        { client.disconnect(); System.err.println("SMTP server refused connection."); System.exit(1); }

        client.setSender(sender);
        client.addRecipient(recipient);

        Writer writer = client.sendMessageData();
        if (writer != null)

        { writer.write(header.toString()); writer.write(body); writer.close(); client.completePendingCommand(); }

        client.logout();

        client.disconnect();
        }
        catch (Exception e)

        { e.printStackTrace(); System.exit(1); }

        }
        }

        Show
        Houman Atashbar added a comment - This is how you might use it: public final class SMTPSMail { public final static void main(String[] args) { String sender = "src.email@gmail.com"; String senderXoauthToken = "R0VUIGh0dHBzOi8vbWFpbC5nb29nbGUuY29tL21haWwvYi9yZWxheW1lLnRpbnl3ZWJnZWFyc0BnbWFpbC5jb20vc210cC8gb2F1dGhfc2lnbmF0dXJlPSJIekpxMFlVdGlwZ3FVTHk3bWVqcCUyRktJYlg3OCUzRCIsc2NvcGU9Imh0dHBzJTNBJTJGJTJGd3d3Lmdvb2dsZWFwaXMuY29tJTJGYXV0aCUyRnVzZXJpbmZvLmVtYWlsJTIwaHR0cHMlM0ElMkYlMkZtYWlsLmdvb2dsZS5jb20lMkYiLG9hdXRoX3ZlcnNpb249IjEuMCIsb2F1dGhfbm9uY2U9IjgzODI2MDUzNyIsb2F1dGhfc2lnbmF0dXJlX21ldGhvZD0iSE1BQy1TSEExIixvYXV0aF9jb25zdW1lcl9rZXk9InJlbGF5bWUudGlueXdlYmdlYXJzLmNvbSIsb2F1dGhfdG9rZW49IjElMkZHVDA0QmxaOWVwYjk1WE10QURSSjNsNEd6b1R4YTJlR3BlYmxvZXRpT0ZvIixvYXV0aF90aW1lc3RhbXA9IjEzNDQ3NDI2MTki"; String recipient = "destination.email@gmail.com"; String subject = "Testing Commons Net"; String body = "Testing at " + new Date(); String server = "smtp.gmail.com"; Integer port = 587; try { SimpleSMTPHeader header = new SimpleSMTPHeader(sender, recipient, subject); AuthenticatingSMTPClient client = new AuthenticatingSMTPClient(); client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true)); client.connect(server, port); client.login(); client.execTLS(); client.auth(AuthenticatingSMTPClient.AUTH_METHOD.XOAUTH, senderXoauthToken, null); if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) { client.disconnect(); System.err.println("SMTP server refused connection."); System.exit(1); } client.setSender(sender); client.addRecipient(recipient); Writer writer = client.sendMessageData(); if (writer != null) { writer.write(header.toString()); writer.write(body); writer.close(); client.completePendingCommand(); } client.logout(); client.disconnect(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }
        Hide
        Houman Atashbar added a comment -

        How to create XOAUTH strings (for Google accounts): https://developers.google.com/google-apps/gmail/oauth_protocol

        Show
        Houman Atashbar added a comment - How to create XOAUTH strings (for Google accounts): https://developers.google.com/google-apps/gmail/oauth_protocol
        Hide
        Sebb added a comment -

        It would be slightly more consistent with other methods if the code accepted a plain string and applied the Bas64 encoding itself.

        Also, maybe there needs to be a method to create the string from the URL, nonce, timestamp etc?
        At present, the code assumes that the string can be generated externally.
        Is the XOAUTH string created in the same way for other SMTP/IMAP providers, changing only the URL etc values?
        Or are there different parameters used by different servers?

        Show
        Sebb added a comment - It would be slightly more consistent with other methods if the code accepted a plain string and applied the Bas64 encoding itself. Also, maybe there needs to be a method to create the string from the URL, nonce, timestamp etc? At present, the code assumes that the string can be generated externally. Is the XOAUTH string created in the same way for other SMTP/IMAP providers, changing only the URL etc values? Or are there different parameters used by different servers?
        Hide
        Houman Atashbar added a comment -

        Hi Sebb, thanks for your quick reply.
        I haven't looked at the requirements for other servers, but for Google itself it is different between 1.0a and 2 versions (add to this 2-legged OAuth which doesn't require a token). This is while it doesn't matter for commons-net since it uses the XOAUTH string the same way no matter what the protocol is. So, I think it would be nice if there was such a helper but I would do it conceptually separate to the authentication method itself.
        I have no strong opinion on either plain or Base64, or even passing a map of key values used to create this string. If it is more consistent to accept a plain text, so be it.

        Show
        Houman Atashbar added a comment - Hi Sebb, thanks for your quick reply. I haven't looked at the requirements for other servers, but for Google itself it is different between 1.0a and 2 versions (add to this 2-legged OAuth which doesn't require a token). This is while it doesn't matter for commons-net since it uses the XOAUTH string the same way no matter what the protocol is. So, I think it would be nice if there was such a helper but I would do it conceptually separate to the authentication method itself. I have no strong opinion on either plain or Base64, or even passing a map of key values used to create this string. If it is more consistent to accept a plain text, so be it.
        Hide
        Sebb added a comment -

        URL: http://svn.apache.org/viewvc?rev=1396374&view=rev
        Log:
        NET-482 Support XOAUTH

        Modified:
        commons/proper/net/trunk/src/changes/changes.xml
        commons/proper/net/trunk/src/main/java/org/apache/commons/net/imap/AuthenticatingIMAPClient.java
        commons/proper/net/trunk/src/main/java/org/apache/commons/net/imap/IMAPCommand.java
        commons/proper/net/trunk/src/main/java/org/apache/commons/net/smtp/AuthenticatingSMTPClient.java

        Show
        Sebb added a comment - URL: http://svn.apache.org/viewvc?rev=1396374&view=rev Log: NET-482 Support XOAUTH Modified: commons/proper/net/trunk/src/changes/changes.xml commons/proper/net/trunk/src/main/java/org/apache/commons/net/imap/AuthenticatingIMAPClient.java commons/proper/net/trunk/src/main/java/org/apache/commons/net/imap/IMAPCommand.java commons/proper/net/trunk/src/main/java/org/apache/commons/net/smtp/AuthenticatingSMTPClient.java
        Hide
        Houman Atashbar added a comment -

        Thanks Sebb,
        This is awesome.

        Show
        Houman Atashbar added a comment - Thanks Sebb, This is awesome.

          People

          • Assignee:
            Unassigned
            Reporter:
            Houman Atashbar
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development