本文整理汇总了Java中javax.mail.BodyPart.setHeader方法的典型用法代码示例。如果您正苦于以下问题:Java BodyPart.setHeader方法的具体用法?Java BodyPart.setHeader怎么用?Java BodyPart.setHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.BodyPart
的用法示例。
在下文中一共展示了BodyPart.setHeader方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: populateContentOnBodyPart
import javax.mail.BodyPart; //导入方法依赖的package包/类
protected String populateContentOnBodyPart(BodyPart part, MailConfiguration configuration, Exchange exchange)
throws MessagingException, IOException {
String contentType = determineContentType(configuration, exchange);
if (contentType != null) {
LOG.trace("Using Content-Type {} for BodyPart: {}", contentType, part);
// always store content in a byte array data store to avoid various content type and charset issues
String data = exchange.getContext().getTypeConverter().tryConvertTo(String.class, exchange.getIn().getBody());
// use empty data if the body was null for some reason (otherwise there is a NPE)
data = data != null ? data : "";
DataSource ds = new ByteArrayDataSource(data, contentType);
part.setDataHandler(new DataHandler(ds));
// set the content type header afterwards
part.setHeader("Content-Type", contentType);
}
return contentType;
}
示例2: addAttachment
import javax.mail.BodyPart; //导入方法依赖的package包/类
@Override
protected void addAttachment(String name, DataHandler data) throws MessagingException {
BodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(data);
attachment.setFileName(name);
attachment.setHeader("Content-ID", "<" + name + ">");
iBody.addBodyPart(attachment);
}
示例3: resolveContentTransferEncoding
import javax.mail.BodyPart; //导入方法依赖的package包/类
protected void resolveContentTransferEncoding(AttachmentsContentTransferEncodingResolver resolver, int i, BodyPart messageBodyPart) throws MessagingException {
if (resolver != null) {
String contentTransferEncoding = resolver.resolveContentTransferEncoding(messageBodyPart);
LOG.trace("Attachment #{}: Using content transfer encoding resolver: {} resolved content transfer encoding as: {}", i, resolver, contentTransferEncoding);
if (contentTransferEncoding != null) {
messageBodyPart.setHeader("Content-Transfer-Encoding", contentTransferEncoding);
}
}
}
示例4: addAttachment
import javax.mail.BodyPart; //导入方法依赖的package包/类
private void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
messageBodyPart.setHeader("Content-ID","<image>");
_multipart.addBodyPart(messageBodyPart);
}
示例5: getBodyPartFromDatasource
import javax.mail.BodyPart; //导入方法依赖的package包/类
/**
* Helper method which generates a {@link BodyPart} from an {@link AttachmentResource} (from its {@link DataSource}) and a disposition type
* ({@link Part#INLINE} or {@link Part#ATTACHMENT}). With this the attachment data can be converted into objects that fit in the email structure.
* <p>
* For every attachment and embedded image a header needs to be set.
*
* @param attachmentResource An object that describes the attachment and contains the actual content data.
* @param dispositionType The type of attachment, {@link Part#INLINE} or {@link Part#ATTACHMENT} .
*
* @return An object with the attachment data read for placement in the email structure.
* @throws MessagingException All BodyPart setters.
*/
private static BodyPart getBodyPartFromDatasource(final AttachmentResource attachmentResource, final String dispositionType)
throws MessagingException {
final BodyPart attachmentPart = new MimeBodyPart();
// setting headers isn't working nicely using the javax mail API, so let's do that manually
final String resourceName = determineResourceName(attachmentResource, false);
final String fileName = determineResourceName(attachmentResource, true);
attachmentPart.setDataHandler(new DataHandler(new NamedDataSource(fileName, attachmentResource.getDataSource())));
attachmentPart.setFileName(fileName);
final String contentType = attachmentResource.getDataSource().getContentType();
attachmentPart.setHeader("Content-Type", contentType + "; filename=" + fileName + "; name=" + resourceName);
attachmentPart.setHeader("Content-ID", format("<%s>", resourceName));
attachmentPart.setDisposition(dispositionType);
return attachmentPart;
}
示例6: send
import javax.mail.BodyPart; //导入方法依赖的package包/类
/**
* Send this email message.
*
* @param session Java Mail session providing connectivity to the outgoing mal server.
* @throws MessagingException If any errors occur that prevent the sending of this message.
*/
public void send(final Session session) throws MessagingException {
Message message = new MimeMessage(session);
message.setFrom(from);
message.setRecipients(Message.RecipientType.TO, to.toArray(new Address[0]));
message.setRecipients(Message.RecipientType.CC, cc.toArray(new Address[0]));
message.setRecipients(Message.RecipientType.BCC, bcc.toArray(new Address[0]));
message.setSubject(subject);
if (imageDataSources.isEmpty()) {
message.setContent(messageBody, "text/html");
} else {
Multipart multipart = new MimeMultipart("related");
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(messageBody, "text/html");
multipart.addBodyPart(htmlPart);
for (EmbeddedImageReference imageReference : imageDataSources) {
BodyPart imgPart = new MimeBodyPart();
imgPart.setDataHandler(new DataHandler(imageReference.getImageDataSource()));
imgPart.setHeader("Content-ID", imageReference.getContentId());
multipart.addBodyPart(imgPart);
}
message.setContent(multipart);
}
Transport.send(message);
}
示例7: getMessageByContentTypes
import javax.mail.BodyPart; //导入方法依赖的package包/类
protected Map<String, String> getMessageByContentTypes(Message message, String characterSet) throws Exception {
Map<String, String> messageMap = new HashMap<>();
if (message.isMimeType(TEXT_PLAIN)) {
messageMap.put(TEXT_PLAIN, MimeUtility.decodeText(message.getContent().toString()));
} else if (message.isMimeType(TEXT_HTML)) {
messageMap.put(TEXT_HTML, MimeUtility.decodeText(convertMessage(message.getContent().toString())));
} else if (message.isMimeType(MULTIPART_MIXED) || message.isMimeType(MULTIPART_RELATED)) {
messageMap.put(MULTIPART_MIXED, extractMultipartMixedMessage(message, characterSet));
} else {
Object obj = message.getContent();
Multipart mpart = (Multipart) obj;
for (int i = 0, n = mpart.getCount(); i < n; i++) {
Part part = mpart.getBodyPart(i);
if (decryptMessage && part.getContentType() != null &&
part.getContentType().equals(ENCRYPTED_CONTENT_TYPE)) {
part = decryptPart((MimeBodyPart)part);
}
String disposition = part.getDisposition();
String partContentType = part.getContentType().substring(0, part.getContentType().indexOf(";"));
if (disposition == null) {
if (part.getContent() instanceof MimeMultipart) {
// multipart with attachment
MimeMultipart mm = (MimeMultipart) part.getContent();
for (int j = 0; j < mm.getCount(); j++) {
if (mm.getBodyPart(j).getContent() instanceof String) {
BodyPart bodyPart = mm.getBodyPart(j);
if ((characterSet != null) && (characterSet.trim().length() > 0)) {
String contentType = bodyPart.getHeader(CONTENT_TYPE)[0];
contentType = contentType
.replace(contentType.substring(contentType.indexOf("=") + 1), characterSet);
bodyPart.setHeader(CONTENT_TYPE, contentType);
}
String partContentType1 = bodyPart
.getContentType().substring(0, bodyPart.getContentType().indexOf(";"));
messageMap.put(partContentType1,
MimeUtility.decodeText(bodyPart.getContent().toString()));
}
}
} else {
//multipart - w/o attachment
//if the user has specified a certain characterSet we decode his way
if ((characterSet != null) && (characterSet.trim().length() > 0)) {
InputStream istream = part.getInputStream();
ByteArrayInputStream bis = new ByteArrayInputStream(ASCIIUtility.getBytes(istream));
int count = bis.available();
byte[] bytes = new byte[count];
count = bis.read(bytes, 0, count);
messageMap.put(partContentType,
MimeUtility.decodeText(new String(bytes, 0, count, characterSet)));
} else {
messageMap.put(partContentType, MimeUtility.decodeText(part.getContent().toString()));
}
}
}
} //for
} //else
return messageMap;
}
示例8: addAttachmentsToMultipart
import javax.mail.BodyPart; //导入方法依赖的package包/类
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, DataHandler> entry : exchange.getIn().getAttachments().entrySet()) {
String attachmentFilename = entry.getKey();
DataHandler handler = entry.getValue();
if (LOG.isTraceEnabled()) {
LOG.trace("Attachment #{}: Disposition: {}", i, partDisposition);
LOG.trace("Attachment #{}: DataHandler: {}", i, handler);
LOG.trace("Attachment #{}: FileName: {}", i, attachmentFilename);
}
if (handler != null) {
if (shouldAddAttachment(exchange, attachmentFilename, handler)) {
// Create another body part
BodyPart messageBodyPart = new MimeBodyPart();
// Set the data handler to the attachment
messageBodyPart.setDataHandler(handler);
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 +++");
}
示例9: sendInvoice
import javax.mail.BodyPart; //导入方法依赖的package包/类
public static void sendInvoice(Invoice factuur, byte[] invoiceBuf, User user) throws Exception {
loadProperties();
String to = factuur.getEmail();
String bcc = factuur.getEmailBcc();
String cc = factuur.getEmailCc();
String subj = "Factuur " + factuur.getInvoiceNumber();
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
Message msg = new MimeMessage(session);
InternetAddress[] toAddrs = null, ccAddrs = null, bccAddrs = null;
if (to != null) {
toAddrs = InternetAddress.parse(to, false);
msg.setRecipients(Message.RecipientType.TO, toAddrs);
} else
throw new MessagingException("No \"To\" address specified");
if (cc != null) {
ccAddrs = InternetAddress.parse(cc, false);
msg.setRecipients(Message.RecipientType.CC, ccAddrs);
}
if (bcc != null) {
bccAddrs = InternetAddress.parse(bcc, false);
msg.setRecipients(Message.RecipientType.BCC, bccAddrs);
} else {
bccAddrs = InternetAddress.parse(user.getEmail(), false);
msg.setRecipients(Message.RecipientType.BCC, bccAddrs);
}
if (subj != null)
msg.setSubject(subj);
msg.setFrom(new InternetAddress(user.getCompanyName() + " <" + user.getEmail() + ">"));
MimeMultipart multipart = new MimeMultipart("related");
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "Zie bijlage.";
messageBodyPart.setContent(htmlText, "text/html");
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource fds = new ByteArrayDataSource(invoiceBuf, "application/pdf");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<application/pdf>");
messageBodyPart.setFileName("factuur.pdf");
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
Transport tr = session.getTransport("smtp");
tr.connect(props.getProperty("mail.smtp.host"), 465, props.getProperty("mail.smtp.user"), props.getProperty("mail.smtp.password"));
msg.saveChanges();
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
}
示例10: copyHeadersToPart
import javax.mail.BodyPart; //导入方法依赖的package包/类
private void copyHeadersToPart(Enumeration headers, BodyPart part) throws MessagingException {
while (headers.hasMoreElements()) {
Header header = (Header) headers.nextElement();
part.setHeader(header.getName(), header.getValue());
}
}
示例11: createMessage
import javax.mail.BodyPart; //导入方法依赖的package包/类
/**
* Creates a MIME message from a SOAP message.
*
* @param from the 'from' mail address.
* @param to the 'to' mail address(es).
* @param cc the 'cc' mail address(es).
* @param subject the mail subject.
* @param soapMessage the SOAP message.
* @param session the mail session.
* @return a new MIME message.
* @throws ConnectionException if error occurred in constructing the mail
* message.
* @see hk.hku.cecid.piazza.commons.net.MailSender#createMessage(java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.mail.Session)
*/
public MimeMessage createMessage(String from, String to, String cc,
String subject, SOAPMessage soapMessage, Session session) throws ConnectionException {
try {
MimeMessage message = super.createMessage(from, to, cc, subject, session);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
soapMessage.writeTo(bos);
String contentType = getContentType(soapMessage);
ByteArrayDataSource content = new ByteArrayDataSource(bos.toByteArray(), contentType);
soapMessage.getAttachments();
boolean hasAttachments = soapMessage.countAttachments() > 0;
if (hasAttachments) {
putHeaders(soapMessage.getMimeHeaders(), message);
MimeMultipart mmp = new MimeMultipart(content);
for (int i=0; i<mmp.getCount(); i++) {
BodyPart bp = mmp.getBodyPart(i);
// encoding all parts in base64 to overcome length of character line limitation in SMTP
InputStreamDataSource isds = new InputStreamDataSource(
bp.getInputStream(), bp.getContentType(), bp.getFileName());
bp.setDataHandler(new DataHandler(isds));
bp.setHeader("Content-Transfer-Encoding", "base64");
}
message.setContent(mmp);
}
else {
DataHandler dh = new DataHandler(content);
message.setDataHandler(dh);
message.setHeader("Content-Transfer-Encoding", "base64");
}
message.saveChanges();
return message;
}
catch (Exception e) {
throw new ConnectionException("Unable to construct mail message from SOAP message", e);
}
}