com.thematchbox.email.SendMail Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of TheMatchBox-Common Show documentation
Show all versions of TheMatchBox-Common Show documentation
This project contains some common utilities used in different projects from theMatchBox.
package com.thematchbox.email;
/* Copyright 2015 theMatchBox
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.
*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
public class SendMail {
public static final Logger logger = LoggerFactory.getLogger(SendMail.class);
private final String username;
private final String password;
private final String smtpHost;
private final int smtpPort;
private final boolean useTls;
private final boolean authenticate;
public SendMail(String username, String password, String smtpHost, int smtpPort, boolean useTls, boolean authenticate) {
this.username = username;
this.password = password;
this.smtpHost = smtpHost;
this.smtpPort = smtpPort;
this.useTls = useTls;
this.authenticate = authenticate;
}
public void sendSimpleMail(String emailAddresses, String fromAddress, String ccAddresses, String bccAddresses, String subject, String messageText) throws IOException, MessagingException {
sendSimpleMail(emailAddresses, fromAddress, null, ccAddresses, bccAddresses, subject, messageText);
}
public void sendSimpleMail(String emailAddresses, String fromAddress, String replyToAddress, String ccAddresses, String bccAddresses, String subject, String messageText) throws IOException, MessagingException {
Properties props = new Properties();
props.put("mail.smtp.auth", authenticate);
props.put("mail.smtp.starttls.enable", useTls);
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", smtpPort);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailAddresses));
if (replyToAddress != null) {
message.setReplyTo(InternetAddress.parse(replyToAddress));
}
if (ccAddresses != null) {
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccAddresses));
}
if (bccAddresses != null) {
message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bccAddresses));
}
message.setSentDate(new Date());
message.setSubject(subject);
message.setText(messageText);
Transport.send(message);
logger.debug("Mail sent to " + emailAddresses);
}
}