use of javax.mail.BodyPart in project camel by apache.
the class MailBinding method addBodyToMultipart.
protected void addBodyToMultipart(MailConfiguration configuration, MimeMultipart activeMultipart, Exchange exchange) throws MessagingException, IOException {
BodyPart bodyMessage = new MimeBodyPart();
populateContentOnBodyPart(bodyMessage, configuration, exchange);
activeMultipart.addBodyPart(bodyMessage);
}
use of javax.mail.BodyPart in project camel by apache.
the class MailBinding method addAttachmentsToMultipart.
protected void addAttachmentsToMultipart(MimeMultipart multipart, String partDisposition, AttachmentsContentTransferEncodingResolver encodingResolver, Exchange exchange) throws MessagingException {
LOG.trace("Adding attachments +++ start +++");
int i = 0;
for (Map.Entry<String, Attachment> entry : exchange.getIn().getAttachmentObjects().entrySet()) {
String attachmentFilename = entry.getKey();
Attachment attachment = entry.getValue();
if (LOG.isTraceEnabled()) {
LOG.trace("Attachment #{}: Disposition: {}", i, partDisposition);
LOG.trace("Attachment #{}: DataHandler: {}", i, attachment.getDataHandler());
LOG.trace("Attachment #{}: FileName: {}", i, attachmentFilename);
}
if (attachment != null) {
if (shouldAddAttachment(exchange, attachmentFilename, attachment.getDataHandler())) {
// Create another body part
BodyPart messageBodyPart = new MimeBodyPart();
// Set the data handler to the attachment
messageBodyPart.setDataHandler(attachment.getDataHandler());
// Set headers to the attachment
for (String headerName : attachment.getHeaderNames()) {
List<String> values = attachment.getHeaderAsList(headerName);
for (String value : values) {
messageBodyPart.setHeader(headerName, value);
}
}
if (attachmentFilename.toLowerCase().startsWith("cid:")) {
// add a Content-ID header to the attachment
// must use angle brackets according to RFC: http://www.ietf.org/rfc/rfc2392.txt
messageBodyPart.addHeader("Content-ID", "<" + attachmentFilename.substring(4) + ">");
// Set the filename without the cid
messageBodyPart.setFileName(attachmentFilename.substring(4));
} else {
// Set the filename
messageBodyPart.setFileName(attachmentFilename);
}
LOG.trace("Attachment #" + i + ": ContentType: " + messageBodyPart.getContentType());
if (contentTypeResolver != null) {
String contentType = contentTypeResolver.resolveContentType(attachmentFilename);
LOG.trace("Attachment #" + i + ": Using content type resolver: " + contentTypeResolver + " resolved content type as: " + contentType);
if (contentType != null) {
String value = contentType + "; name=" + attachmentFilename;
messageBodyPart.setHeader("Content-Type", value);
LOG.trace("Attachment #" + i + ": ContentType: " + messageBodyPart.getContentType());
}
}
// set Content-Transfer-Encoding using resolver if possible
resolveContentTransferEncoding(encodingResolver, i, messageBodyPart);
// Set Disposition
messageBodyPart.setDisposition(partDisposition);
// Add part to multipart
multipart.addBodyPart(messageBodyPart);
} else {
LOG.trace("shouldAddAttachment: false");
}
} else {
LOG.warn("Cannot add attachment: " + attachmentFilename + " as DataHandler is null");
}
i++;
}
LOG.trace("Adding attachments +++ done +++");
}
use of javax.mail.BodyPart in project camel by apache.
the class MailBinding method extractHeadersFromMail.
protected Map<String, Object> extractHeadersFromMail(Message mailMessage, Exchange exchange) throws MessagingException, IOException {
Map<String, Object> answer = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
Enumeration<?> names = mailMessage.getAllHeaders();
while (names.hasMoreElements()) {
Header header = (Header) names.nextElement();
String value = header.getValue();
if (headerFilterStrategy != null && !headerFilterStrategy.applyFilterToExternalHeaders(header.getName(), value, exchange)) {
CollectionHelper.appendValue(answer, header.getName(), value);
}
}
// if the message is a multipart message, do not set the content type to multipart/*
if (((MailEndpoint) exchange.getFromEndpoint()).getConfiguration().isMapMailMessage()) {
Object content = mailMessage.getContent();
if (content instanceof MimeMultipart) {
MimeMultipart multipart = (MimeMultipart) content;
int size = multipart.getCount();
for (int i = 0; i < size; i++) {
BodyPart part = multipart.getBodyPart(i);
content = part.getContent();
// in case of nested multiparts iterate into them
while (content instanceof MimeMultipart) {
if (multipart.getCount() < 1) {
break;
}
part = ((MimeMultipart) content).getBodyPart(0);
content = part.getContent();
}
if (part.getContentType().toLowerCase().startsWith("text")) {
answer.put(Exchange.CONTENT_TYPE, part.getContentType());
break;
}
}
}
}
return answer;
}
use of javax.mail.BodyPart in project camel by apache.
the class MailBinding method createMultipartAlternativeMessage.
protected void createMultipartAlternativeMessage(MimeMessage mimeMessage, MailConfiguration configuration, Exchange exchange) throws MessagingException, IOException {
MimeMultipart multipartAlternative = new MimeMultipart("alternative");
mimeMessage.setContent(multipartAlternative);
MimeBodyPart plainText = new MimeBodyPart();
plainText.setText(getAlternativeBody(configuration, exchange), determineCharSet(configuration, exchange));
// remove the header with the alternative mail now that we got it
// otherwise it might end up twice in the mail reader
exchange.getIn().removeHeader(configuration.getAlternativeBodyHeader());
multipartAlternative.addBodyPart(plainText);
// if there are no attachments, add the body to the same mulitpart message
if (!exchange.getIn().hasAttachments()) {
addBodyToMultipart(configuration, multipartAlternative, exchange);
} else {
// treat them as normal. It will append a multipart-mixed with the attachments and the body text
if (!configuration.isUseInlineAttachments()) {
BodyPart mixedAttachments = new MimeBodyPart();
mixedAttachments.setContent(createMixedMultipartAttachments(configuration, exchange));
multipartAlternative.addBodyPart(mixedAttachments);
} else {
// if the attachments are set to be inline, attach them as inline attachments
MimeMultipart multipartRelated = new MimeMultipart("related");
BodyPart related = new MimeBodyPart();
related.setContent(multipartRelated);
multipartAlternative.addBodyPart(related);
addBodyToMultipart(configuration, multipartRelated, exchange);
AttachmentsContentTransferEncodingResolver resolver = configuration.getAttachmentsContentTransferEncodingResolver();
addAttachmentsToMultipart(multipartRelated, Part.INLINE, resolver, exchange);
}
}
}
use of javax.mail.BodyPart in project KJFrameForAndroid by kymjs.
the class SimpleMailSender method sendHtmlMail.
/**
* 以HTML格式发送邮件
*
* @param mailInfo
* 待发送的邮件信息
*/
public static boolean sendHtmlMail(MailSenderInfo mailInfo) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
// 如果需要身份认证,则创建一个密码验证器
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
// Message.RecipientType.TO属性表示接收者的类型为TO
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
Aggregations