Search in sources :

Example 1 with SimpleSMTPHeader

use of org.apache.commons.net.smtp.SimpleSMTPHeader in project network-monitor by caarmen.

the class Emailer method sendEmail.

/**
 * Sends an e-mail in UTF-8 encoding.
 *
 * @param protocol    this has been tested with "TLS", "SSL", and null.
 * @param attachments optional attachments to include in the mail.
 */
static void sendEmail(String protocol, String server, int port, String user, String password, String from, String[] recipients, String subject, String body, Set<File> attachments) throws Exception {
    // Set up the mail connectivity
    final AuthenticatingSMTPClient client;
    if (protocol == null)
        client = new AuthenticatingSMTPClient();
    else
        client = new AuthenticatingSMTPClient(protocol);
    if (BuildConfig.DEBUG)
        client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
    client.setDefaultTimeout(SMTP_TIMEOUT_MS);
    client.setCharset(Charset.forName(ENCODING));
    client.connect(server, port);
    checkReply(client);
    client.helo("[" + client.getLocalAddress().getHostAddress() + "]");
    checkReply(client);
    if ("TLS".equals(protocol)) {
        if (!client.execTLS()) {
            checkReply(client);
            throw new RuntimeException("Could not start tls");
        }
    }
    client.auth(AuthenticatingSMTPClient.AUTH_METHOD.LOGIN, user, password);
    checkReply(client);
    // Set up the mail participants
    client.setSender(from);
    checkReply(client);
    for (String recipient : recipients) {
        client.addRecipient(recipient);
        checkReply(client);
    }
    // Set up the mail content
    Writer writer = client.sendMessageData();
    SimpleSMTPHeader header = new SimpleSMTPHeader(from, recipients[0], subject);
    for (int i = 1; i < recipients.length; i++) header.addCC(recipients[i]);
    // Just plain text mail: no attachments
    if (attachments == null || attachments.isEmpty()) {
        header.addHeaderField("Content-Type", "text/plain; charset=" + ENCODING);
        writer.write(header.toString());
        writer.write(body);
    } else // Mail with attachments
    {
        String boundary = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 28);
        header.addHeaderField("Content-Type", "multipart/mixed; boundary=" + boundary);
        writer.write(header.toString());
        // Write the main text message
        writer.write("--" + boundary + "\n");
        writer.write("Content-Type: text/plain; charset=" + ENCODING + "\n\n");
        writer.write(body);
        writer.write("\n");
        // Write the attachments
        appendAttachments(writer, boundary, attachments);
        writer.write("--" + boundary + "--\n\n");
    }
    writer.close();
    if (!client.completePendingCommand()) {
        throw new RuntimeException("Could not send mail");
    }
    client.logout();
    client.disconnect();
}
Also used : AuthenticatingSMTPClient(org.apache.commons.net.smtp.AuthenticatingSMTPClient) PrintCommandListener(org.apache.commons.net.PrintCommandListener) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) SimpleSMTPHeader(org.apache.commons.net.smtp.SimpleSMTPHeader) PrintWriter(java.io.PrintWriter)

Aggregations

PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 PrintCommandListener (org.apache.commons.net.PrintCommandListener)1 AuthenticatingSMTPClient (org.apache.commons.net.smtp.AuthenticatingSMTPClient)1 SimpleSMTPHeader (org.apache.commons.net.smtp.SimpleSMTPHeader)1