File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Send jsp page in mail as attachment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Send jsp page in mail as attachment" Watch "Send jsp page in mail as attachment" New topic

Send jsp page in mail as attachment

Varsha Goswami

Joined: Jul 20, 2009
Posts: 16
I need to send a jsp page as an attachment in mail.
I am using this part in code....
But using this I am getting the entire html script in the sent mail...
Can anybody help me out...??

Properties props = System.getProperties();
String mailServer = "";

props.put("", mailServer);

Logger logger = Logger.getLogger(PropertyDashboardAction.class);

// Get a mail session
Session session1 = Session.getDefaultInstance(props, null);

String from = "";
String to = "";
// Define a new mail message
Message message = new MimeMessage(session1);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Test mail");

// Create a message part to represent the body text
BodyPart messageBodyPart = new MimeBodyPart();

//use a MimeMultipart as we need to handle the file attachments
Multipart multipart = new MimeMultipart();

//add the message body to the mime message

// add any file attachments to the message
//addAtachments(attachments, multipart);

// Put all message parts in the message

// Send the message

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
What do you mean by "send a JSP page"? JSPs exist only in the context of a servlet container; outside of it, they are just files. Or do you mean to send an HTML page that was created by a JSP?

Also, what is "emailStuff"?
Varsha Goswami

Joined: Jul 20, 2009
Posts: 16
I mean to say that I want to send the jsp in html form attached in a mail ... so that if a user is looking at a screen and on click of an email option he can mail that screen to any id...

code for emailstuff is here...

if(null!=printExport && printExport.equalsIgnoreCase("email")){
String emailTemplateJSP = "/jsp/propertyMgmt/propertylandingpageEmail.jsp";
RequestDispatcher rd = servlet.getServletContext().getRequestDispatcher(emailTemplateJSP);
rd.include(request, response);

// Get the generated email from the request attribute
String emailStuff = (String)request.getAttribute("test");
Jaydeep Mazumdar

Joined: Aug 26, 2009
Posts: 23
I remember implementing this for one of my projects.

There is a custom tag available using which you can store the output of the JSP (i.e. HTML page) in a HttpSession attribute. I dont remember the name but you can google for the same.

You can then retrieve the contents of the HttpSession attribute and then add it as a content or as attachment.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
I think it would be much simpler to just send the URL of the web page the user is looking at. Many people are unwilling to receive HTML content, or even have email clients that are unable to do so. And even if both are not the case, it'll be very tricky to obtain decent results (e.g., many email clients block images in HTML emails).
I agree. Here's the link:
subject: Send jsp page in mail as attachment
It's not a secret anymore!