Solved

JSP, javamail attachments

Posted on 2007-04-03
2
705 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
Comment Utility
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
Comment Utility
Thank you for your response.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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…
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 …
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.

771 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

11 Experts available now in Live!

Get 1:1 Help Now