Java tutorial
/* * Copyright 2013 ANTEAM 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.anteam.alert.email.service; import java.util.Date; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message.RecipientType; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import com.anteam.alert.email.model.EmailPasswordAuthenticator; import com.anteam.alter.core.model.AntlertMessage; import com.anteam.alter.core.service.IAnteamAlert; /** * <p> * ?? * </p> * * @author <a href="mailto:wsysisibeibei@gmail.com">sisibeibei</a> * @date Oct 22, 2013 9:32:35 AM */ public class EmailAntlert implements IAnteamAlert, InitializingBean { private static final String CONTENT_MIME_TYPE = "text/html;charset=UTF-8"; private static final String CHARSET = "UTF-8"; private static Logger logger = LoggerFactory.getLogger(EmailAntlert.class); private InternetAddress sender; private EmailPasswordAuthenticator mailPwdAuther; private Session mailSession; /** * */ private InternetAddress[] receivers; /** * mail server ?SMTPAuth */ private Properties mailProps; public EmailAntlert() { super(); } @Override public boolean send(AntlertMessage antlertMsg) { MimeMessage mimeMsg; // MIME // from?to? mimeMsg = new javax.mail.internet.MimeMessage(mailSession); try { // ? mimeMsg.setFrom(sender); // mimeMsg.setRecipients(RecipientType.TO, receivers); // mimeMsg.setSubject(antlertMsg.getTitle(), CHARSET); // MimeBodyPart messageBody = new MimeBodyPart(); messageBody.setContent(antlertMsg.getContent(), CONTENT_MIME_TYPE); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBody); mimeMsg.setContent(multipart); // ?? mimeMsg.setSentDate(new Date()); mimeMsg.saveChanges(); // ?? Transport.send(mimeMsg); } catch (MessagingException e) { logger.error("???", e); return false; } return true; } @Override public void afterPropertiesSet() throws Exception { // ? mailSession = javax.mail.Session.getInstance(mailProps, (Authenticator) mailPwdAuther); } public InternetAddress getSender() { return sender; } public void setSender(InternetAddress sender) { this.sender = sender; } public EmailPasswordAuthenticator getMailPwdAuther() { return mailPwdAuther; } public void setMailPwdAuther(EmailPasswordAuthenticator mailPwdAuther) { this.mailPwdAuther = mailPwdAuther; } public InternetAddress[] getReceivers() { return receivers; } public void setReceivers(InternetAddress[] receivers) { this.receivers = receivers; } public Properties getMailProps() { return mailProps; } public void setMailProps(Properties mailProps) { this.mailProps = mailProps; } }