Sup fellas...another question..how Can I pass my jsp as the contents of a email message.

The way we send email around here is we set the parameters of a form into a DB table and a cron job sends the emails out

String address= "suggestaservice@agentadvantage.com"        String name = request.getParameter("name");
	String accountName = userData.getAcctName();
	String email = request.getParameter("email");
	String description = request.getParameter("description");
	String benefit = request.getParameter("benefit");
   
	
	//This is the body of the email
	String message = "Enhancement suggestion from:" + "\n\n";
	message += "Name: " + name + "\n";
	message += "Email: " + email + "\n";
	message += "Account: " + username + "\n\n";
	message += "Description: " + description + "\n\n";
    message += "Benefit: " + benefit;
	
   OutgoingMessage msg = new OutgoingMessage();
   msg.setFromAddr(email);
   msg.setFromName(name);
   msg.setToAddr(address);
   msg.setSubject("Preferred Pages Enhancement Suggestion");
   msg.setContent(message);
   msg.send();

any suggestions on how to set a whole form as the "message"

thanx

Dani AI

Generated

Short answer: render the JSP to HTML and use that HTML as the email body (don’t attach raw JSP source). was right that a JSP is just text, but for a usable email send the rendered output. Two common approaches are (A) capture the JSP output inside the webapp and hand that string to the mailer, or (B) use a template engine (FreeMarker/Velocity) so the cron job can render the message without the servlet stack.

A minimal pattern to capture a JSP from inside a servlet/container is to include it into a response wrapper that writes into a StringWriter:

class StringResponseWrapper extends HttpServletResponseWrapper {
  private StringWriter sw = new StringWriter();
  private PrintWriter pw = new PrintWriter(sw);
  public StringResponseWrapper(HttpServletResponse resp){ super(resp); }
  @Override public PrintWriter getWriter(){ return pw; }
  public String getOutput(){ pw.flush(); return sw.toString(); }
}

StringResponseWrapper wrap = new StringResponseWrapper(response);
request.getRequestDispatcher("/emailTemplate.jsp").include(request, wrap);
String htmlBody = wrap.getOutput();

Send that htmlBody as an HTML MIME part (JavaMail example):

MimeMessage m = new MimeMessage(session);
m.setFrom(new InternetAddress(from));
m.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
m.setSubject(subject, "UTF-8");

MimeMultipart alt = new MimeMultipart("alternative");
MimeBodyPart plain = new MimeBodyPart();
plain.setText(plainText, "UTF-8");
MimeBodyPart html = new MimeBodyPart();
html.setContent(htmlBody, "text/html; charset=UTF-8");
alt.addBodyPart(plain); alt.addBodyPart(html);
m.setContent(alt);
Transport.send(m);

Notes and gotchas: if the cron sender runs outside the servlet container, either fetch the rendered JSP via an HTTP GET or switch to a template engine so rendering is independent of the web request. Inline CSS, avoid scripts, use absolute URLs for images (or embed them as CID attachments), and include a plain-text alternative. Test across major mail clients and watch SPF/DKIM settings so messages don’t land in spam.

Recommended Answers

All 2 Replies

mimsc,
>how Can I pass my jsp as the contents of a email?

Yes, after all jsp page is also a text file. Attachment of jsp page or assign the content of jsp to message body.

attachment to message body? ...a little confused as to how to do that

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.