Solved

JSP, javamail attachments

Posted on 2007-04-03
2
707 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fix34  challenge 9 105
seriesUp challenge 7 129
copyEndy  challenge 15 85
Books that can get me started on JAVA 2 81
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now