Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

email dynamic image  RSS feed

 
David C. Meyers
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm attempting to send an email with an image. The image could be an attachment, or it could be imbedded. I don't have a preference. I am reading the image from a database and through a third party API I get a Point[]. I use the point array to create a BufferedImage. I'd like to try to attach the BufferedImage to the email. I have found sample code to attach an image from a file, but I'd rather not write my image to a file. My problem would be solved if I could create a DataSource from a Buffered image. I'd appreciate any advice.

// code to create the BufferedImage
Point[] pointArray = ...;

BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);

Graphics2D g2 = image.createGraphics();
g2.setColor(Color.white);
g2.fillRect(0, 0, 200, 200);

g2.setColor(Color.black);

Point lastPoint = null;
for (int i = 0; i < pointArray.length; i++)
{
if (-1 == pointArray[i].x && -1 == pointArray[i].y)
{
lastPoint = null;
continue;
}

if (null != lastPoint)
{
g2.drawLine(lastPoint.x, lastPoint.y, pointArray[i].x, pointArray[i].y);
}

lastPoint = pointArray[i];
}

g2.dispose();
// end BufferedImage Code


// code to send a message with attachment
// from http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#SendingAttachments
// Create the message
Message message = new MimeMessage(session);

// Fill its headers
message.setSubject("Embedded Image");
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));

// Create your new message part
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Hello</H1>" +
"<img src=\"cid:memememe\">";
messageBodyPart.setContent(htmlText, "text/html");

// Create a related multi-part to combine the parts
MimeMultipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart);

// Create part for the image
messageBodyPart = new MimeBodyPart();

// Fetch the image and associate to part
DataSource fds = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","<memememe>");

// Add part to multi-part
multipart.addBodyPart(messageBodyPart);

// Associate multi-part with message
message.setContent(multipart);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!