Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 04:51 PM
Member
 
Join Date: Jul 2007
Posts: 40
lenny is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-25-2007, 03:49 PM
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 01:15 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org