use of fi.otavanopisto.muikku.mail.MailType in project muikku by otavanopisto.
the class JndiMailService method onMailEvent.
@Asynchronous
@Lock(LockType.READ)
public void onMailEvent(@Observes JndiMailEvent event) {
try {
MailType mimeType = event.getMimeType();
String from = event.getFrom();
List<String> to = event.getTo();
List<String> cc = event.getCc();
List<String> bcc = event.getBcc();
String subject = event.getSubject();
String content = event.getContent();
List<MailAttachment> attachments = event.getAttachments();
Properties props = new Properties();
InitialContext initialContext = new InitialContext(props);
Session mailSession = (Session) initialContext.lookup("java:/mail/muikku");
MimeMessage message = new MimeMessage(mailSession);
if (!to.isEmpty()) {
message.setRecipients(Message.RecipientType.TO, parseToAddressArray(to));
}
if (!cc.isEmpty()) {
message.setRecipients(Message.RecipientType.CC, parseToAddressArray(cc));
}
if (!bcc.isEmpty()) {
message.setRecipients(Message.RecipientType.BCC, parseToAddressArray(bcc));
}
if (from != null) {
InternetAddress fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
message.setReplyTo(new InternetAddress[] { fromAddress });
}
if (subject != null) {
message.setSubject(subject);
}
message.setSentDate(new Date());
if (!attachments.isEmpty()) {
Multipart multipart = new MimeMultipart();
if (content != null) {
MimeBodyPart contentBodyPart = new MimeBodyPart();
contentBodyPart.setContent(content, mimeType.toString());
multipart.addBodyPart(contentBodyPart);
}
for (MailAttachment attachment : attachments) {
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setContent(attachment.getContent(), attachment.getMimeType());
attachmentBodyPart.setFileName(attachment.getFileName());
multipart.addBodyPart(attachmentBodyPart);
}
message.setContent(multipart);
} else {
if (content != null) {
message.setContent(content, mimeType.toString());
}
}
// Sending
Transport.send(message);
} catch (Exception e) {
logger.log(Level.SEVERE, "Error sending mail", e);
}
}
Aggregations