Java tutorial
/* * Copyright (C) 2017 Baifendian Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.baifendian.swordfish.common.mail; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Collection; import java.util.Properties; import org.apache.commons.lang3.StringUtils; import org.apache.commons.mail.HtmlEmail; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.ResourceUtils; public class MailSendUtil { private static Logger LOGGER = LoggerFactory.getLogger(MailSendUtil.class.getName()); private static String mailProtocol; private static String mailServerHost; private static Integer mailServerPort; private static String mailSender; private static String mailPasswd; static { try { File dataSourceFile = ResourceUtils.getFile("classpath:common/mail.properties"); InputStream is = new FileInputStream(dataSourceFile); Properties properties = new Properties(); properties.load(is); mailProtocol = properties.getProperty("mail.protocol"); mailServerHost = properties.getProperty("mail.server.host"); mailServerPort = Integer.parseInt(properties.getProperty("mail.server.port")); mailSender = properties.getProperty("mail.sender"); mailPasswd = properties.getProperty("mail.passwd"); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } /** * ?? * * @param receivers * @param title * @param content * @return */ public static boolean sendMails(Collection<String> receivers, String title, String content) { if (receivers == null) { LOGGER.error("Mail receivers is null."); return false; } receivers.removeIf((from) -> (StringUtils.isEmpty(from))); if (receivers.isEmpty()) { LOGGER.error("Mail receivers is empty."); return false; } // ?? email HtmlEmail email = new HtmlEmail(); try { // SMTP ?????, 163 "smtp.163.com" email.setHostName(mailServerHost); email.setSmtpPort(mailServerPort); // ? email.setCharset("UTF-8"); // for (String receiver : receivers) { email.addTo(receiver); } // ?? email.setFrom(mailSender, mailSender); // ???????-?????? email.setAuthentication(mailSender, mailPasswd); // ??? email.setSubject(title); // ???? HtmlEmail? HTML email.setMsg(content); // ?? email.send(); return true; } catch (Throwable e) { LOGGER.error("Send email to {} failed", StringUtils.join(",", receivers), e); } return false; } }