JavaMail
is the recommended way to send e-mail from Java code on our servers.
We keep the latest versions of JavaMail and the JavaBeans Activation Framework in your classpath,
so you do not need to install these into your site.
The following sample JSP demonstrates the correct approach for sending e-mail from your Java code:
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<html>
<head><title>JavaMail Test</title></head>
<body>
<%
Properties props = new Properties(); // Properties for this mail session
Session mailSession; // Current mail session
Transport transport; // Mail transport protocol object
// Initialize props, mailSession, transport
props.put("mail.smtp.host", "localhost");
mailSession = Session.getInstance(props, null);
transport = mailSession.getTransport("smtp");
String fromAddress = "from@yourdomain.com";
String toAddress = "to@somedomain.com";
String subject = "Test message subject";
String body = "Test message body";
// Construct and send the e-mail message
MimeMessage msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(fromAddress));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
msg.setSentDate(new Date());
msg.setSubject(subject);
msg.setText(body);
// Send the message
transport.send(msg);
%>
If you don't see an exception report, then the message was sent successfully.
</body>
</html>
|