开发者

what's sendmail equivalent in JSP?

开发者 https://www.devze.com 2023-03-28 02:25 出处:网络
I\'m trying to s开发者_如何学Cend an email out in JSP, but it looks like I have to set SMTP server manually unlike PHP (PHP uses sendmail).

I'm trying to s开发者_如何学Cend 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);
0

精彩评论

暂无评论...
验证码 换一张
取 消