View Single Post
  #2 (permalink)  
Old 07-25-2007, 03:49 PM
shanePreater shanePreater is offline
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
HI,
Yes this is quite straight forward when using the javax.mail package:
Here is a simple (Although not well error trapped or tested!) example
Code:
/** * */ package com.onevisionsupport.com.mailing.example; import java.io.File; import java.net.URL; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.Message.RecipientType; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.xml.bind.PropertyException; /** * @author shanepreater * */ public class ImageAttachedSender { public static void main(String[] args) { //Define the properties to allow you to connect to your mail server. try { Properties mailProps = new Properties(); mailProps.setProperty("mail.smtp.host", "example-smtp.onevisionsupport.com"); //First create a mail session Session session = Session.getInstance(mailProps); //Create the main message object MimeMessage message = new MimeMessage(session); //Create the sender and recipients for the message. InternetAddress from = new InternetAddress("info@onevisionsupport.com"); InternetAddress to = new InternetAddress("student@somewhere.com"); setupMainDetails(message, from, to); //Create the holder for our message. Basically needed to hold the body parts (text bit and image bit). Multipart multipart = new MimeMultipart("related"); //Add this to the message. message.setContent(multipart); //Create the text bit of the body BodyPart textPart = createTextPart(); multipart.addBodyPart(textPart); //Create the image attachment. BodyPart imagePart = createImagePart(); //Now add the image part. multipart.addBodyPart(imagePart); Transport.send(message); } catch (AddressException e) { // TODO Provide better error handling in proper code. e.printStackTrace(); } catch (MessagingException e) { // TODO Provide better error handling in proper code. e.printStackTrace(); } } /** * @return * @throws MessagingException */ private static BodyPart createImagePart() throws MessagingException { BodyPart imagePart = new MimeBodyPart(); File imageFile = findImageFile(); DataSource fds = new FileDataSource (imageFile); imagePart.setDataHandler(new DataHandler(fds)); imagePart.setHeader("Content-ID","<image>"); return imagePart; } /** * @return */ private static File findImageFile() { //Build a File object to the actual image. URL resource = Thread.currentThread().getContextClassLoader().getResource("com/onevisionsupport/com/mailing/example/ovsl-logo.gif"); return new File(resource.toExternalForm().replaceAll("%20", " ")); } /** * @param message * @param from * @param to * @throws MessagingException */ private static void setupMainDetails(MimeMessage message, InternetAddress from, InternetAddress to) throws MessagingException { //Setup the main message details from, to and subject message.setSubject("Attaching image example!"); message.setFrom(from); message.addRecipient(RecipientType.TO, to); } /** * @return * @throws MessagingException */ private static BodyPart createTextPart() throws MessagingException { BodyPart textPart = new MimeBodyPart(); textPart.setText("Please see attached image. Thanks"); return textPart; } }
It should be pretty self exlainatory but if not then pop a reply on and I will explain further.

I will (attempt) to put the gif up as well which you will need to put in the same package as the class (also remember to paste the class into the correct package com.onevisionsupport.com.mailing.example).

If I fail to attach the gif you can get it from http://www.onevisionsupport.com/images/logo.gif

Hope this helps.
Attached Images
File Type: gif ovsl-logo.gif (3.0 KB, 1 views)
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Reply With Quote