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;
}
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);
}
}
}
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);
}
Aggregations