• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

email dynamic image

David C. Meyers
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.fillRect(0, 0, 200, 200);


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

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

lastPoint = pointArray[i];

// 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));
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");

// 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));

// Add part to multi-part

// Associate multi-part with message
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic