Ex 7 JSP
Ex 7 JSP
html
AIM:
ALGORITHM STEPS:
Step 1
Step 2
Step 3
Step 4
Step 5
This page is created to receive the values from the user, like mail-id, subject and message
content that
they want to send. Then we send these details to the "mailJSP.jsp" page to
respond depending on them.html
Step 6
Now create a new JSP page "mailJSP.jsp" and write the following code for it. In the
comment line I'll provide you a short description of the use of each attribute.
Step 7
Step 8
Now provide the detail there as "mail-id", "subject-line" and
"message-content" as in the following that I
provided.
Step 9
words something is missing, then recheck your JSP code and try again else you will get the
same
Step 10
For confirmation of the mail go to the receive mail inbox (those mail-ids that you provide in
the sending
page) or go to your mail-id and check the sent mail items. As in the following.
RESULT:
Thus the JSP program for sending a mail to the friends have been executed successfully.
<!DOCTYPE html>
<html>
<head>
<title>Sending Mail Through JSP</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body bgcolor="khaki">
<form action="mailJSP.jsp">
<table>
<tr><td><b><font color="red">To:
</td>
<td><b><b><input type="text" name="mail" value="Enter sender mail-id"/><br/>
</td>
</tr>
<tr>
<td>
<b><font color="red">Subject:
</td>
<td>
<input type="text" name="sub" value="Enter Subject Line"><br/>
</td>
</tr>
<tr>
<td>
<b><font color="red">Message Text:
</td>
<td>
<textarea rows="12" cols="80" name="mess"></textarea><br/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Send">
</td>
<td>
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</body>
</html>
mailJSP.jsp
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*" %>
<%
//Creating a result for getting status that messsage is delivered or not!
String result;
// Get recipient's email-ID, message & subject-line from index.html page
final String to = request.getParameter("mail");
final String subject = request.getParameter("sub");
final String messg = request.getParameter("mess");
// Defining properties
props.put("mail.smtp.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.user", from);
props.put("mail.password", pass);
props.put("mail.port", "465");
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(mailSession);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject(subject);
// Now set the actual message
message.setText(messg);
// Send message
Transport.send(message);
result = "Your mail sent successfully....";
} catch (MessagingException mex) {
mex.printStackTrace();
result = "Error: unable to send mail....";
}
%>
<title>Sending Mail in JSP</title>
<h1><center><font color="blue">Sending Mail Using JSP</font></h1>
<b><center><font color="red"><% out.println(result);%></b>