|
JSP service may be used to send mail messages. For this first example we will create a simple JSP that
sends an email. In order to send mail the application should have JavaMail and JAF packages in its class path.
This example uses hardcoded value for source and recipient address, but it could be easilly changed
to a HTML form input.
You can use your own email address so that you may check for the sent mail. You should also replace the "smtp.mail.example.com" value with your own SMTP email server:
<%@page import="java.util.*"%>
<%@page import="javax.mail.*"%>
<%@page import="javax.mail.internet.*"%>
<%
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.example.com");
Session s = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(s);
InternetAddress from = new InternetAddress("
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
");
message.setFrom(from);
InternetAddress to = new InternetAddress("
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
");
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>
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.