2

I need to send an email using SSL (SMTPS) and authentification. In apache Commons Net however there seems to be either AuthenticatingSMTPClient (no SSL, though it extends SMTPSClient?) or SMTPSClient (no authentication?), I need a combination of both (SSL + authentication). Anyone knows how I can do this? Thanks!

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270

1 Answers1

4

I know it is too late to reply to this but for future reference for others, AuthenticatingSMTPClient does provide ssl + authentication as it is extending SMTPSClient. Below is the sample code, one has to do modifications where I have commented with numerals (e.g. //1).

Code:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.io.Util;
import org.apache.commons.net.smtp.AuthenticatingSMTPClient;
import org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD;
import org.apache.commons.net.smtp.SMTPReply;
import org.apache.commons.net.smtp.SimpleSMTPHeader;

public final class SMTPMail
{
    public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException
    {
        String sender, recipient, subject, filename, server;
        List<String> ccList = new ArrayList<String>();
        FileReader fileReader = null;
        Writer writer;
        SimpleSMTPHeader header;
        AuthenticatingSMTPClient client;

        server = "<smtp server>"; // 1
        try
        {
            sender = "<your user name>"; // 2
            recipient = "<recipient>"; // 3
            subject = "<mail subject>"; // 4

            header = new SimpleSMTPHeader(sender, recipient, subject);
            filename = "hello.txt"; //This will be the body of your mail //5

            try
            {
                fileReader = new FileReader(filename);
            }
            catch (FileNotFoundException e)
            {
                System.err.println("File not found. " + e.getMessage());
            }

            client = new AuthenticatingSMTPClient("TLS", false);
            client.addProtocolCommandListener(new PrintCommandListener(
                    new PrintWriter(System.out), true));

            client.connect(server);

            if (!SMTPReply.isPositiveCompletion(client.getReplyCode()))
            {
                client.disconnect();
                System.err.println("SMTP server refused connection.");
                System.exit(1);
            }

            client.login("hostname.testing.smtp.api"); //6

            if(client.execTLS())
            {
                if(client.auth(AUTH_METHOD.LOGIN, "<your user name>", "<your password>")) //7
                {
                    client.setSender(sender);
                    client.addRecipient(recipient);
                    for (String recpt : ccList) {
                        client.addRecipient(recpt);
                    }

                    writer = client.sendMessageData();

                    if (writer != null)
                    {
                        writer.write(header.toString());
                        Util.copyReader(fileReader, writer);
                        writer.close();
                        client.completePendingCommand();
                    }

                    if (fileReader != null ) {
                        fileReader.close();
                    }
                }
            }
            client.logout();
            client.disconnect();
        }
        catch (IOException e)
        {
            e.printStackTrace();
            System.exit(1);
        }
    }
}
Favonius
  • 13,959
  • 3
  • 55
  • 95
  • I think it should be client.elogin not client.login otherwise it will send "HELO" instead of "EHLO" and the execTLS will hit an error "503 AUTH command used when not advertised". You will get away with it with smtp.gmail.com but not with many other email servers – rich Dec 19 '16 at 15:11
  • .connect should also specify the TLS port as simply setting the protocol to TLS when you initiate the AuthenticatingSMTPClient doesn't necessarily point it at port 587. – rich Dec 19 '16 at 16:11