me.at.coding
me.at.coding

Reputation: 17796

Use AuthenticatingSMTPClient and SSL?

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!

Upvotes: 2

Views: 1105

Answers (1)

Favonius
Favonius

Reputation: 13984

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);
        }
    }
}

Upvotes: 4

Related Questions