Solved

JSP, javamail attachments

Posted on 2007-04-03
2
710 Views
Last Modified: 2013-11-24
Hello, I have a jsp page that includes the javamail snippet below. This works fine, but I need to know how to modify this to include one or more attachments. For example, I have the files;
c:\myFile.txt AND c:\myImage.gif that I would like to include. Any help is appreciated!

<%
        Properties props = new Properties();
      props.put("mail.host", "mailsrv");
      Session s = Session.getInstance(props,null);

      MimeMessage message = new MimeMessage(s);

      InternetAddress from = new InternetAddress("myweb@toothbrush.com");
      message.setFrom(from);
      
         InternetAddress[] addresses={
        new InternetAddress("sammy@yourweb.com"),
        new InternetAddress("greg@hisweb.com")};      
 
        message.setRecipients(Message.RecipientType.TO, addresses);
        message.setSubject("MY MAIL TEST);
        message.setText("This is my message");
      
       Transport.send(message);
%>
0
Comment
Question by:ramoore
2 Comments
 
LVL 10

Accepted Solution

by:
Jaax earned 125 total points
ID: 18848369
Sample code from
http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailAttachments:

// Define message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
  new InternetAddress(to));
message.setSubject("Hello JavaMail Attachment");

// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
messageBodyPart.setText("Pardon Ideas");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

// Send the message
Transport.send(message);
0
 

Author Comment

by:ramoore
ID: 18902055
Thank you for your response.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 29
ejb entity bean example issue 2 18
servlet and mdb, jms error 1 39
Setup GlassFish 4 22
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question