简体   繁体   中英

what's sendmail equivalent in JSP?

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?

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/

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM