Java tutorial
/** * Copyright (C) 2013 Seajas, the Netherlands. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.seajas.search.attender.service.mail; import java.net.URI; import java.net.URISyntaxException; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.apache.cxf.common.util.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.MailException; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import com.seajas.search.attender.model.setting.MailSettings; import com.seajas.search.attender.service.attender.AttenderService; import com.seajas.search.utilities.logging.SearchLogger; /** * Mail service. * * @author Jasper van Veghel <jasper@seajas.com> */ @Service public class MailService { /** * The logger. */ @Autowired private SearchLogger logger; /** * Mail sender address. */ @Value("${attender.project.mail.from.address}") private String fromAddress; /** * Number of results per message. */ @Value("${attender.project.mail.results.per.message}") private Integer resultsPerMessage; /** * Mail sender. */ @Autowired private JavaMailSender sender; /** * Attender service. */ @Autowired private AttenderService attenderService; /** * Retrieve the resultsPerMessage. * * @return Integer */ public Integer getResultsPerMessage() { return resultsPerMessage; } /** * Send an e-mail message. * * @param receiver * @param textContent * @param htmlContent */ public boolean sendMessage(final String receiver, final String subject, final String textContent, final String htmlContent) { MimeMessage message = sender.createMimeMessage(); // We request a multi-part message so we can write out both HTML and text try { MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setFrom(fromAddress); helper.setTo(receiver); helper.setSubject(subject); helper.setText(textContent, htmlContent); sender.send(message); return true; } catch (MessagingException e) { logger.error("Could not build the given e-mail message for receiver '" + receiver + "'", e); } catch (MailException e) { logger.error("Could not send the givn e-mail message for receiver '" + receiver + "'", e); } return false; } /** * Determine whether a mail server was added. * * @return boolean */ public boolean hasWorkingMailServer() { return !StringUtils.isEmpty(((JavaMailSenderImpl) sender).getHost()); } /** * Switch the e-mail sender to the inputted mail server, if applicable. */ public void updateWorkingMailServer() { // Use the mail-server override if one has been entered and validated if (attenderService.validateCurrentMailServerDetails()) try { MailSettings settings = attenderService.getMailServerDetails(); URI hostnameURI = settings.getHostnameUri(); ((JavaMailSenderImpl) sender).setHost(hostnameURI.getHost()); Integer schemeStandardPort = "smtps".equals(hostnameURI.getScheme()) ? 465 : 25; ((JavaMailSenderImpl) sender) .setPort(hostnameURI.getPort() != -1 ? hostnameURI.getPort() : schemeStandardPort); if (!StringUtils.isEmpty(settings.getUsername())) ((JavaMailSenderImpl) sender).setUsername(settings.getUsername()); if (!StringUtils.isEmpty(settings.getPassword())) ((JavaMailSenderImpl) sender).setPassword(settings.getPassword()); ((JavaMailSenderImpl) sender).setProtocol(hostnameURI.getScheme()); if (StringUtils.isEmpty(settings.getUsername()) && StringUtils.isEmpty(settings.getPassword())) ((JavaMailSenderImpl) sender).getJavaMailProperties() .setProperty("mail." + hostnameURI.getScheme() + ".auth", "true"); fromAddress = settings.getSender(); } catch (URISyntaxException e) { logger.error("Could not set the given mail settings."); } } }