use of javax.activation.DataSource in project TranskribusCore by Transkribus.
the class SendMail method createMultiPartMessage.
public static Multipart createMultiPartMessage(String message, File[] atts) throws MessagingException {
MimeBodyPart messageBodyPart = new MimeBodyPart();
// fill message
messageBodyPart.setText(message);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
for (File f : atts) {
DataSource source = new FileDataSource(f);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(f.getName());
multipart.addBodyPart(messageBodyPart);
}
return multipart;
}
use of javax.activation.DataSource in project serverless by bluenimble.
the class SmtpMessenger method send.
@Override
public void send(Sender sender, Recipient[] recipients, String subject, String content, ApiStreamSource... attachments) throws MessengerException {
ClassLoader pcl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
// creates a new e-mail message
try {
if (sender == null) {
sender = NoSender;
}
Message message = new MimeMessage(session);
String senderEmail = sender.id();
if (Lang.isNullOrEmpty(senderEmail)) {
senderEmail = user;
}
String senderName = sender.name();
if (Lang.isNullOrEmpty(senderName)) {
message.setFrom(new InternetAddress(senderEmail));
} else {
message.setFrom(new InternetAddress(senderEmail, senderName));
}
InternetAddress[] toAddresses = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
toAddresses[i] = new InternetAddress(recipients[i].id());
}
message.setRecipients(Message.RecipientType.TO, toAddresses);
message.setSubject(subject);
message.setSentDate(new Date());
// creates message part
MimeBodyPart messageText = new MimeBodyPart();
messageText.setContent(content, ApiContentTypes.Html);
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageText);
// adds attachments
if (attachments != null && attachments.length > 0) {
for (final ApiStreamSource attachment : attachments) {
MimeBodyPart mbd = new MimeBodyPart();
DataSource ds = new DataSource() {
@Override
public String getContentType() {
return attachment.contentType();
}
@Override
public InputStream getInputStream() throws IOException {
return attachment.stream();
}
@Override
public String getName() {
return attachment.name();
}
@Override
public OutputStream getOutputStream() throws IOException {
throw new UnsupportedOperationException("getOutputStream not supported");
}
};
mbd.setDataHandler(new DataHandler(ds));
multipart.addBodyPart(mbd);
}
}
// sets the multi-part as e-mail's content
message.setContent(multipart);
// sends the e-mail
Transport.send(message);
} catch (Exception ex) {
throw new MessengerException(ex.getMessage(), ex);
} finally {
Thread.currentThread().setContextClassLoader(pcl);
}
}
use of javax.activation.DataSource in project i2p.i2p-bote by i2p.
the class MultipartFilter method processRequest.
/**
* Saves all uploaded files in a request to the temporary directory so
* they can be attached to an email.<br/>
* All other request parameters are passed down the filter chain as a
* new request.
* @param request
* @return a new <code>HttpServletRequest</code>
* @throws IOException
* @throws ServletException
*/
private HttpServletRequest processRequest(final HttpServletRequest request) throws IOException, ServletException {
// only process multipart requests
if (!isMultipart(request))
return request;
DataSource dataSource = new DataSource() {
@Override
public OutputStream getOutputStream() throws IOException {
return null;
}
@Override
public String getName() {
return "HTTP request data source";
}
@Override
public InputStream getInputStream() throws IOException {
return request.getInputStream();
}
@Override
public String getContentType() {
return request.getContentType();
}
};
Map<String, String[]> nonFileParameters = new HashMap<String, String[]>();
try {
// <nasty hack>
// JavaMail assumes ISO-8859-1 for text parts when parsing multipart data.
// There seems to be no easy way to override this behavior, or to change the
// content type of a BodyPart. Since the ISO-8859-1 encoding will mess up
// non-ascii UTF-8 characters, we change all "text/plain" parts to
// "text/plain; charset=UTF-8" to keep JavaMail from using the ISO-8859-1
// default.
// See also com.sun.mail.handlers.text_plain.getCharset(String)
MimeMultipart multipart = new MimeMultipart(dataSource) {
@Override
public BodyPart getBodyPart(int index) throws MessagingException {
BodyPart part = super.getBodyPart(index);
part.setDataHandler(new DataHandler(new MimePartDataSource((MimeBodyPart) part) {
@Override
public String getContentType() {
String contentType = super.getContentType();
if ("text/plain".equals(contentType))
return "text/plain; charset=UTF-8";
else
return contentType;
}
}));
return part;
}
};
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
String origFilename = bodyPart.getFileName();
if (origFilename != null && origFilename.length() > 0) {
// write the temporary file
InputStream input = bodyPart.getInputStream();
File tempFile = File.createTempFile("i2pbote_attachment_", ".tmp");
// under normal circumstances, the temp files are deleted after the email is added to the outbox
tempFile.deleteOnExit();
FileOutputStream output = new FileOutputStream(tempFile);
try {
Util.copy(input, output);
} finally {
output.close();
}
UploadedFile uploadedFile = new UploadedFile(origFilename, tempFile.getAbsolutePath());
String paramName = getParamName(bodyPart);
request.setAttribute(paramName, uploadedFile);
} else {
// not a file, add the parameter to the map
String paramName = getParamName(bodyPart);
Object content = bodyPart.getContent();
String paramValue = content == null ? "" : content.toString();
add(nonFileParameters, paramName, paramValue);
}
}
} catch (MessagingException e) {
throw new ServletException("Can't write uploaded data to a temp file.", e);
}
// Wrap the request with the parameter map which we just created and return it.
return wrapRequest(request, nonFileParameters);
}
use of javax.activation.DataSource in project quickstarts by jboss-switchyard.
the class InternalEchoServiceBean method echoImage.
@Override
public String echoImage(String fileName) {
String newFileName = "external-switchyard.png";
DataSource image = message.getAttachment(fileName);
// Something is wrong in Camel it throws StackOverFlow error.
// message.removeAttachment(fileName);
message.addAttachment(newFileName, image);
newFileName = _echoService.echoImage(newFileName);
return newFileName;
}
use of javax.activation.DataSource in project orientdb by orientechnologies.
the class OMailPlugin method addAttachment.
/**
* Adds a file as an attachment to the email's content
*
* @param multipart
* @param filePath
* @throws MessagingException
*/
private void addAttachment(final Multipart multipart, final String filePath) throws MessagingException {
MimeBodyPart attachPart = new MimeBodyPart();
DataSource source = new FileDataSource(filePath);
attachPart.setDataHandler(new DataHandler(source));
attachPart.setFileName(new File(filePath).getName());
multipart.addBodyPart(attachPart);
}
Aggregations