Results 1 to 2 of 2
Thread: Send a pic through mail, in java
- 07-24-2007, 03:51 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
- 07-25-2007, 02:49 PM #2
Member
- Join Date
- Jul 2007
- Location
- England, Bath
- Posts
- 47
- Rep Power
- 0
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
It should be pretty self exlainatory but if not then pop a reply on and I will explain further.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; } }
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.
Similar Threads
-
JSP send mail script not working.
By profuse in forum Java AppletsReplies: 1Last Post: 05-27-2008, 06:37 AM -
A Client to Send SMTP Mail
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:06 PM -
Retreiving of mail body using mail number
By chandu.v09 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 03-13-2008, 02:25 PM -
java mail server
By krismedia in forum New To JavaReplies: 2Last Post: 01-23-2008, 04:41 AM -
Sending a mail with the local mail program
By thedude in forum Advanced JavaReplies: 2Last Post: 07-23-2007, 12:19 PM


LinkBack URL
About LinkBacks

Bookmarks