Search in sources :

Example 1 with MailAttachment

use of fi.otavanopisto.muikku.mail.MailAttachment in project muikku by otavanopisto.

the class DummyMailBridge method sendMail.

@Override
public void sendMail(MailType mimeType, String from, List<String> to, List<String> cc, List<String> bcc, String subject, String content, List<MailAttachment> attachments) {
    StringBuilder message = new StringBuilder();
    message.append("DUMMY MAIL: Type: ");
    message.append(mimeType.toString());
    message.append(" From: ");
    message.append(from);
    message.append(" To: ");
    for (String email : to) {
        message.append(email);
        message.append(" ");
    }
    message.append("CC: ");
    for (String email : cc) {
        message.append(email);
        message.append(" ");
    }
    message.append("BCC: ");
    for (String email : bcc) {
        message.append(email);
        message.append(" ");
    }
    message.append("Subject: ");
    message.append(subject);
    message.append("Content: ");
    message.append(content);
    message.append(" Attachments: ");
    for (MailAttachment attachment : attachments) {
        message.append(attachment.toString());
        message.append(" ");
    }
    logger.log(Level.INFO, message.toString());
}
Also used : MailAttachment(fi.otavanopisto.muikku.mail.MailAttachment)

Example 2 with MailAttachment

use of fi.otavanopisto.muikku.mail.MailAttachment 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);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MailAttachment(fi.otavanopisto.muikku.mail.MailAttachment) MailType(fi.otavanopisto.muikku.mail.MailType) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) Date(java.util.Date) AddressException(javax.mail.internet.AddressException) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session) Asynchronous(javax.ejb.Asynchronous) Lock(javax.ejb.Lock)

Aggregations

MailAttachment (fi.otavanopisto.muikku.mail.MailAttachment)2 MailType (fi.otavanopisto.muikku.mail.MailType)1 Date (java.util.Date)1 Properties (java.util.Properties)1 Asynchronous (javax.ejb.Asynchronous)1 Lock (javax.ejb.Lock)1 Multipart (javax.mail.Multipart)1 Session (javax.mail.Session)1 AddressException (javax.mail.internet.AddressException)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 MimeMessage (javax.mail.internet.MimeMessage)1 MimeMultipart (javax.mail.internet.MimeMultipart)1 InitialContext (javax.naming.InitialContext)1