81 lines
2.3 KiB
Java
81 lines
2.3 KiB
Java
package org.gcube.applicationsupportlayer.social;
|
|
|
|
import java.util.Date;
|
|
import java.util.Properties;
|
|
|
|
import javax.mail.Address;
|
|
import javax.mail.Message;
|
|
import javax.mail.Session;
|
|
import javax.mail.Transport;
|
|
import javax.mail.internet.InternetAddress;
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
import org.gcube.common.core.utils.logging.GCUBEClientLog;
|
|
|
|
/**
|
|
* A class for sending email
|
|
*
|
|
* @author Massimiliano Assante
|
|
*
|
|
*/
|
|
public class EmailNotification {
|
|
|
|
private static GCUBEClientLog _log = new GCUBEClientLog(EmailNotification.class);
|
|
|
|
private String sender;
|
|
private String recipients[];
|
|
private String subject;
|
|
private String body;
|
|
|
|
/**
|
|
*
|
|
* @param sender
|
|
* @param subject
|
|
* @param body
|
|
* @param recipients
|
|
*/
|
|
public EmailNotification(String sender, String subject, String body, String... recipients) {
|
|
if (recipients == null || recipients.length == 0)
|
|
throw new IllegalArgumentException("There must be at least one recipient");
|
|
this.sender = sender;
|
|
this.recipients = recipients;
|
|
this.subject = subject;
|
|
this.body = body;
|
|
}
|
|
|
|
public void send() {
|
|
Properties props = System.getProperties();
|
|
String mailServiceHost = "localhost";
|
|
props.put("mail.smtp.host", mailServiceHost);
|
|
String mailServicePort = "25";
|
|
props.put("mail.smtp.port", mailServicePort);
|
|
Session session = Session.getDefaultInstance(props, null);
|
|
session.setDebug(true);
|
|
Message mimeMessage = new MimeMessage(session);
|
|
|
|
try {
|
|
// EMAIL SENDER
|
|
Address from = new InternetAddress(sender);
|
|
mimeMessage.setFrom(from);
|
|
|
|
// EMAIL RECIPIENTS
|
|
for (int i=0; i < recipients.length; i++) {
|
|
Address address = new InternetAddress(recipients[i]);
|
|
mimeMessage.addRecipient(Message.RecipientType.TO, address);
|
|
|
|
}
|
|
|
|
mimeMessage.setSubject(subject);
|
|
// mimeMessage.setText(body);
|
|
mimeMessage.setContent(body, "text/html");
|
|
mimeMessage.setSentDate(new Date());
|
|
Transport.send(mimeMessage);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
_log.error("Failed to send the notification email:", e);
|
|
}
|
|
}
|
|
}
|
|
|
|
|