001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.mail;
029
030import org.opencms.main.CmsLog;
031import org.opencms.main.OpenCms;
032import org.opencms.util.CmsStringUtil;
033
034import org.apache.commons.logging.Log;
035import org.apache.commons.mail.EmailException;
036import org.apache.commons.mail.HtmlEmail;
037
038/**
039 * This class is used to send an HTML formatted email with optional attachments.<p>
040 *
041 * A text message can also be set for HTML unaware email clients,
042 * such as text-based email clients.<p>
043 *
044 * It uses the Apache Commons Email API and extends the provided classes
045 * to conveniently generate emails using the OpenCms configuration.<p>
046 *
047 * @since 6.0.0
048 */
049public class CmsHtmlMail extends HtmlEmail {
050
051    /** The log object for this class. */
052    private static final Log LOG = CmsLog.getLog(CmsHtmlMail.class);
053
054    /**
055     * Default constructor of a CmsHtmlMail.<p>
056     *
057     * The mail host name and the mail from address are set to the OpenCms
058     * default values of the configuration.<p>
059     *
060     */
061    public CmsHtmlMail() {
062
063        // call super constructor
064        super();
065        // set the host to the default mail host
066        CmsMailHost host = OpenCms.getSystemInfo().getMailSettings().getDefaultMailHost();
067        setHostName(host.getHostname());
068        setSmtpPort(host.getPort());
069
070        // check if username and password are provided
071        String userName = host.getUsername();
072        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(userName)) {
073            // authentication needed, set user name and password
074            setAuthentication(userName, host.getPassword());
075        }
076        try {
077            // set default mail from address
078            setFrom(OpenCms.getSystemInfo().getMailSettings().getMailFromDefault());
079        } catch (EmailException e) {
080            // default email address is not valid, log error
081            LOG.error(Messages.get().getBundle().key(Messages.LOG_INVALID_SENDER_ADDRESS_0), e);
082        }
083    }
084
085}