Results 1 to 2 of 2
  1. #1
    lenny is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Send a pic through mail, in java

    Hi all, I want to send an image with some details to particular users through mail.
    Using java coding is it possible??
    Or i have to use third party tool which can be written in java.
    Thanks.

  2. #2
    shanePreater is offline Member
    Join Date
    Jul 2007
    Location
    England, Bath
    Posts
    47
    Rep Power
    0

    Default

    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
    Java 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 Thumbnails Attached Thumbnails Send a pic through mail, in java-ovsl-logo.gif  

Similar Threads

  1. JSP send mail script not working.
    By profuse in forum Java Applets
    Replies: 1
    Last Post: 05-27-2008, 06:37 AM
  2. A Client to Send SMTP Mail
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:06 PM
  3. Retreiving of mail body using mail number
    By chandu.v09 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-13-2008, 02:25 PM
  4. java mail server
    By krismedia in forum New To Java
    Replies: 2
    Last Post: 01-23-2008, 04:41 AM
  5. Sending a mail with the local mail program
    By thedude in forum Advanced Java
    Replies: 2
    Last Post: 07-23-2007, 12:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •