Monday, January 30, 2012

Embed Image in Email Using java

Following code snippet can be used to send email with image in the HTML body.

Note :- HTML body must be contain image tag as follows.

<img height="55" src="cid:image-id" width="65" />

Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp host ip ");

Session session = Session.getDefaultInstance(properties, null);

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setSentDate(new Date());



MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setDataHandler(new DataHandler(new ByteArrayDataSource(body, "text/html")));

Multipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);



MimeBodyPart imagePart = new MimeBodyPart();
DataSource fds = new FileDataSource("image path ");
imagePart.setDataHandler(new DataHandler(fds));
imagePart.setFileName("logo.jpg");
imagePart.setHeader("Content-ID", "<
image-id>");
imagePart.setDisposition(MimeBodyPart.INLINE);
multipart.addBodyPart(imagePart);



message.setContent(multipart);
Transport.send(message);

Tuesday, January 24, 2012

dwr mapping filter

<filter>
<filter-name>dwrFilter</filter-name>
<filter-class>org.directwebremoting.servlet.DwrWebContextFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>dwrFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>



This filter can be used to redirect request for DWR servlet.