Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Send jsp page in mail as attachment

 
Varsha Goswami
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 = "mail.abc.com";

props.put("mail.smtp.host", mailServer);

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

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

String from = "varsha@abc.com";
String to = "varsha@abc.com";
// 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();
messageBodyPart.setText(emailStuff);

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

//add the message body to the mime message
multipart.addBodyPart(messageBodyPart);

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

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

// Send the message
Transport.send(message);
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic