PL I – ASSIGNMENT 8

ASSIGNMENT NO.8

Title:

Write a program using MongoDB to compose a web news-letter consisting of videos, images, text use python MongoDB interface.

Requirements:

  1. Computer System with Windows/Linux/Open Source Operating System.
  2. JDK
  3. Eclipse / Netbeans (for J2EE)
  4. Web Server (Apache Tomcat /Glassfish)
  5. MongoDB Server and Client

Theory:

This experiment will send web newsletter created using JSP to the desired recipients. The contents will also be stored in MongoDB database.

To send an email using a JSP is simple enough but to start with its necessary to have JavaMail API and Java Activation Framework (JAF) installed on machine. These files can be downloaded free from Oracle’s official website. Download and unzip these files, in the newly created top level directories. Programmer needs to add mail.jar and activation.jar files in java CLASSPATH.

Send a Simple Email: Here is an example to send a simple email from your machine. Here it is assumed that your localhost is connected to the internet and capable enough to send an email. Same time make sure all the jar files from Java Email API package and JAF package ara available in CLASSPATH.

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%
   String result;
   // Recipient's email ID needs to be mentioned.
   String to = "abcd@gmail.com";
   // Sender's email ID needs to be mentioned
   String from = "mcmohd@gmail.com";
   // Assuming you are sending email from localhost
   String host = "localhost";
   // Get system properties object
  Properties properties = System.getProperties();
   // Setup mail server
   properties.setProperty("mail.smtp.host", host);
   // Get the default Session object.
   Session mailSession = Session.getDefaultInstance(properties);
   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("This is the Subject Line!");
      // Now set the actual message
      message.setText("This is actual message");
      // Send message
      Transport.send(message);
      result = "Sent message successfully....";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message....";
   }
%>
<html>
<head>
<title>Send Email using JSP</title>
</head>
<body>
<center>
<h1>Send Email using JSP</h1>
</center>
<p align="center">
<%
   out.println("Result: " + result + "\n");
%>
</p>
</body>
</html>

Now let us put above code in SendEmail.jsp file and call this JSP using URL http://localhost:8080/SendEmail.jsp which would send an email to given email ID abcd@gmail.com and would display following response:

To send an email to multiple recipients, following methods would be used to specify multiple email IDs:

void addRecipients(Message.RecipientType type,Address[] addresses) throws MessagingException

Here is the description of the parameters:

  • type: This would be set to TO, CC or BCC. Here CC represents Carbon Copy and BCC represents Black Carbon Copy. Example Message.RecipientType.TO
  • addresses: This is the array of email ID. You would need to use InternetAddress() method while specifying email IDs

Leave a Reply

Your email address will not be published. Required fields are marked *