Knowledgebase: Java/JSP Hosting Plans
How do I send email from my servlet or java server pages?
Posted by Customer Service on 03 August 2005 01:51 PM
Please use JavaMail package. Please check the following code:

<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<%
String Fr = "webmaster@test.com";
String To = "mk@test.com";
String SMTPhost = "localhost";

Properties props = new Properties();
props.put("mail.smtp.host", SMTPhost);
Session s = Session.getInstance(props,null);

MimeMessage message = new MimeMessage(s);

InternetAddress from = new InternetAddress(Fr);
message.setFrom(from);
InternetAddress to = new InternetAddress(To);
message.addRecipient(Message.RecipientType.TO, to);
message.setSubject("Test from JavaMail.");
message.setText("Hello from JavaMail!");

Transport.send(message);
%>
<html>
<p align="center">A Message has been sent.<br>Check your inbox.</p>
<p align="center"><a href="sendmail.jsp">Click here to send another!</a></p>
</html>

Here are some links for API documentation and short JavaMAil tutorial:

http://www.jspinsider.com/content/jsp/javamail/jspjavamail_intro.jsp
http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html

Please note, it's important to use 'localhost' as SMTP host without any
authentication, in any application of any kind on your server. As all other
ways are blocked by us, to prevent spam and uncontrolled mass email sending.

If you have any further questions, please don't hesitate to submit a trouble ticket.
(685 vote(s))
Helpful
Not helpful

Comments (0)