0

I'm trying to send an email out in JSP, but it looks like I have to set SMTP server manually unlike PHP (PHP uses sendmail).

What options do I have with JSP?

Moon
  • 22,195
  • 68
  • 188
  • 269
  • Sendmail is not a SMTP server. It still requires a SMTP server. Java's equivalent is JavaMail. As to your question, what container are you targeting? Full fledged appservers such as Glassfish ships with bundled JavaMail API. – BalusC Aug 15 '11 at 23:07

2 Answers2

2

Your best bet, for pure JSP is to just use Java for the email, but the better approach is to write your own tag for sending emails, as I think putting so much code into a JSP page is a bad design.

Here is a nice article with more code, but the basic idea will follow:

http://www.java-samples.com/showtutorial.php?tutorialid=675

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setText(messageText);

Transport.send(msg);

For an article that is possibly a bit dated, but should give you enough information to do it yourself, on JSP tags and email you can read through this:

http://java.sun.com/developer/technicalArticles/javaserverpages/emailapps/

James Black
  • 41,583
  • 10
  • 86
  • 166
0

In Java app servers, you can access smtp servers in 2 basic ways:

Via JNDI lookup, if a mail server is configured in your app server (following example is for JBoss):

Session ms = (Session) new InitialContext().lookup("java:/Mail");

Via directly setting up a Session:

Properties props = new Properties();
props.setProperty("mail.smtp.host", "mySmtpHost");
session = Session.getInstance(props);
atrain
  • 9,139
  • 1
  • 36
  • 40