Search in sources :

Example 1 with RecipientType

use of javax.mail.Message.RecipientType in project jbpm by kiegroup.

the class SendHtml method fillMessage.

private static Message fillMessage(Email email, Session session) {
    org.jbpm.process.workitem.email.Message message = email.getMessage();
    String subject = message.getSubject();
    String from = message.getFrom();
    String replyTo = message.getReplyTo();
    String mailer = "sendhtml";
    if (from == null) {
        throw new RuntimeException("Email must have 'from' address");
    }
    if (replyTo == null) {
        replyTo = from;
    }
    // Construct and fill the Message
    Message msg = null;
    try {
        msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        msg.setReplyTo(new InternetAddress[] { new InternetAddress(replyTo) });
        for (Recipient recipient : message.getRecipients().getRecipients()) {
            RecipientType type = null;
            if ("To".equals(recipient.getType())) {
                type = Message.RecipientType.TO;
            } else if ("Cc".equals(recipient.getType())) {
                type = Message.RecipientType.CC;
            } else if ("Bcc".equals(recipient.getType())) {
                type = Message.RecipientType.BCC;
            } else {
                throw new RuntimeException("Unable to determine recipient type");
            }
            msg.addRecipients(type, InternetAddress.parse(recipient.getEmail(), false));
        }
        if (message.hasAttachment()) {
            Multipart multipart = new MimeMultipart();
            // prepare body as first mime body part
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(message.getBody(), "text/html")));
            multipart.addBodyPart(messageBodyPart);
            List<String> attachments = message.getAttachments();
            for (String attachment : attachments) {
                MimeBodyPart attachementBodyPart = new MimeBodyPart();
                URL attachmentUrl = getAttachemntURL(attachment);
                String contentType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(attachmentUrl.getFile());
                attachementBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(attachmentUrl.openStream(), contentType)));
                String fileName = new File(attachmentUrl.getFile()).getName();
                attachementBodyPart.setFileName(fileName);
                attachementBodyPart.setContentID("<" + fileName + ">");
                multipart.addBodyPart(attachementBodyPart);
            }
            // Put parts in message
            msg.setContent(multipart);
        } else {
            msg.setDataHandler(new DataHandler(new ByteArrayDataSource(message.getBody(), "text/html")));
        }
        msg.setSubject(subject);
        msg.setHeader("X-Mailer", mailer);
        msg.setSentDate(new Date());
    } catch (Exception e) {
        throw new RuntimeException("Unable to send email", e);
    }
    return msg;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) DataHandler(javax.activation.DataHandler) RecipientType(javax.mail.Message.RecipientType) URL(java.net.URL) Date(java.util.Date) MalformedURLException(java.net.MalformedURLException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) NamingException(javax.naming.NamingException) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) File(java.io.File)

Example 2 with RecipientType

use of javax.mail.Message.RecipientType in project zm-mailbox by Zimbra.

the class Mime method removeRecipients.

/**
 * Remove all email addresses in rcpts from To/Cc/Bcc headers of a
 * MimeMessage.
 * @param mm
 * @param rcpts
 * @throws MessagingException
 */
public static void removeRecipients(MimeMessage mm, String[] rcpts) throws MessagingException {
    for (RecipientType rcptType : sRcptTypes) {
        Address[] addrs = mm.getRecipients(rcptType);
        if (addrs == null)
            continue;
        ArrayList<InternetAddress> list = new ArrayList<InternetAddress>(addrs.length);
        for (int j = 0; j < addrs.length; j++) {
            InternetAddress inetAddr = (InternetAddress) addrs[j];
            String addr = inetAddr.getAddress();
            boolean match = false;
            for (int k = 0; k < rcpts.length; k++) if (addr.equalsIgnoreCase(rcpts[k]))
                match = true;
            if (!match)
                list.add(inetAddr);
        }
        if (list.size() < addrs.length) {
            InternetAddress[] newRcpts = new InternetAddress[list.size()];
            list.toArray(newRcpts);
            mm.setRecipients(rcptType, newRcpts);
        }
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ArrayList(java.util.ArrayList) RecipientType(javax.mail.Message.RecipientType)

Example 3 with RecipientType

use of javax.mail.Message.RecipientType in project zm-mailbox by Zimbra.

the class ForwardCalendarItem method makeFwdMsg.

private static Pair<MimeMessage, MimeMessage> makeFwdMsg(Account senderAcct, Invite inv, MimeMessage mmInv, ZVCalendar cal, MimeMessage mmFwdWrapper, MimeBodyPart plainDesc, MimeBodyPart htmlDesc, boolean useFwdText) throws ServiceException, MessagingException, IOException {
    // Set SENT-BY to sender's email address.  Required by Outlook.
    // Also, set ATTENDEEs to the forwardees.  For consistency with Outlook.
    setSentByAndAttendees(cal, AccountUtil.getFriendlyEmailAddress(senderAcct).getAddress(), mmFwdWrapper.getAllRecipients());
    // From: and Sender: headers
    Address from = null;
    Address sender = null;
    sender = AccountUtil.getFriendlyEmailAddress(senderAcct);
    ZOrganizer org = inv.getOrganizer();
    if (org != null) {
        if (org.hasCn())
            from = new JavaMailInternetAddress(org.getAddress(), org.getCn(), MimeConstants.P_CHARSET_UTF8);
        else
            from = new JavaMailInternetAddress(org.getAddress());
    } else {
        from = sender;
    }
    MimeMessage mm;
    if (useFwdText) {
        String plainDescStr = null;
        String htmlDescStr = null;
        if (plainDesc != null)
            plainDescStr = (String) plainDesc.getContent();
        if (htmlDesc != null)
            htmlDescStr = (String) htmlDesc.getContent();
        setDescProps(cal, plainDescStr, htmlDescStr);
        mm = createMergedMessage(senderAcct, from, sender, mmInv, inv, cal, plainDesc, htmlDesc);
    } else {
        mm = CalendarMailSender.createCalendarMessage(senderAcct, from, sender, null, mmInv, inv, cal, false);
    }
    // Copy recipient headers from forward wrapper msg.
    RecipientType[] rcptTypes = { RecipientType.TO, RecipientType.CC, RecipientType.BCC };
    for (RecipientType rcptType : rcptTypes) {
        Address[] rcpts = mmFwdWrapper.getRecipients(rcptType);
        mm.setRecipients(rcptType, rcpts);
    }
    mm.setSubject(mmFwdWrapper.getSubject());
    mm.saveChanges();
    // Create a Forward Notification message if the invitation is originally from ZCS user.
    MimeMessage notifyMimeMsg = null;
    if (org != null) {
        String orgAddress = org.getAddress();
        Account orgAccount = Provisioning.getInstance().getAccountByName(orgAddress);
        if (orgAccount != null && !orgAccount.getId().equals(senderAcct.getId())) {
            notifyMimeMsg = CalendarMailSender.createForwardNotifyMessage(senderAcct, orgAccount, orgAddress, mmFwdWrapper.getAllRecipients(), inv);
        }
    }
    return new Pair<MimeMessage, MimeMessage>(mm, notifyMimeMsg);
}
Also used : Account(com.zimbra.cs.account.Account) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ZOrganizer(com.zimbra.cs.mailbox.calendar.ZOrganizer) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) RecipientType(javax.mail.Message.RecipientType) Pair(com.zimbra.common.util.Pair)

Aggregations

RecipientType (javax.mail.Message.RecipientType)3 InternetAddress (javax.mail.internet.InternetAddress)3 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)2 Address (javax.mail.Address)2 MimeMessage (javax.mail.internet.MimeMessage)2 Pair (com.zimbra.common.util.Pair)1 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)1 Account (com.zimbra.cs.account.Account)1 ZOrganizer (com.zimbra.cs.mailbox.calendar.ZOrganizer)1 File (java.io.File)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 DataHandler (javax.activation.DataHandler)1 Message (javax.mail.Message)1 MessagingException (javax.mail.MessagingException)1 Multipart (javax.mail.Multipart)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1