Solved

JSP, javamail attachments

Posted on 2007-04-03
2
712 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

691 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