Package org.chaidb.db.helper

Source Code of org.chaidb.db.helper.MailUtil

/*
* Copyright (C) 2006  http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
*/
package org.chaidb.db.helper;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;
import org.apache.log4j.Logger;

public class MailUtil {
    private static final boolean DEBUG = false;
    private static final String CONFIG_USER = "mail.smtp.user";
    private static final String CONFIG_PASSWORD = "mail.smtp.password";
    private static final String CONFIG_HOST = "mail.smtp.host";
    private static final String CONFIG_FROM = "mail.from";
    private static final String CONFIG_TO = "mail.to";
    private static final String CONFIG_AUTH = "mail.smtp.auth";
    private static final String FROM = "support@chaidb.com";
    private static final String DEFAULT_HOST = "localhost";
    private static final Logger logger = Logger.getLogger(MailUtil.class);

    public MailUtil() {
    }


    public static void sendTextMail(String from, String to, String subject, String content) {
        sendMail(from, to, null, null, subject, content, false);
    }


    public static void sendMail(String from, String to, String cc, String bcc, String subject, String content, boolean html) {
        try {
            String username = Config.getConfig(CONFIG_USER);
            String password = Config.getConfig(CONFIG_PASSWORD);
            String host = Config.getConfig(CONFIG_HOST, DEFAULT_HOST);
            String needAuth = Config.getConfig(CONFIG_AUTH);
            if (DEBUG) {
                System.out.println(username);
                System.out.println(password);
                System.out.println(host);
                System.out.println(needAuth);
            }

            Email email;
            if (html) {
                email = new HtmlEmail();
            } else {
                email = new SimpleEmail();
            }
            email.setHostName(host);
            if (needAuth.equalsIgnoreCase("true")) {
                email.setAuthentication(username, password);
            }
            email.addTo(to);
            if (cc != null && cc.length() > 0) email.addCc(cc);
            if (bcc != null && bcc.length() > 0) email.addBcc(bcc);
            email.setFrom(from, "ChaiDB Server");
            email.setSubject(subject);

            if (html) {
                ((HtmlEmail) email).setHtmlMsg(content);
            } else {
                email.setMsg(content);
            }
            email.send();
        } catch (Exception exception) {
            logger.error(exception);
            if (DEBUG) {
                System.out.println(exception.getMessage());
                exception.printStackTrace();
            }
        }
    }

    public static void notify(String content) {
        notify("ChaiDB Server Notification", content);
    }

    public static void notify(String subject, String content) {
        String to = Config.getConfig(CONFIG_TO);
        String from = Config.getConfig(CONFIG_FROM, FROM);
        if (DEBUG) {
            System.out.println("from:" + from);
            System.out.println("to:" + to);
        }
        sendTextMail(from, to, subject, content);
    }

    public static void main(String[] args) {
        notify("test", "this is a test mail from chaidb");
    }
}
TOP

Related Classes of org.chaidb.db.helper.MailUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.