Group project
In groups of 2 you are going to do pair programming, you are going to create a email application (cant be applet, for security reasons). That will have at a minimum a to field a subject and message and then send that to the receipt. You will need to start with code below (also have the mail.jar file included - see here)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import java.io.*;
import javax.activation.*;
import javax.imageio.ImageIO;
/**
* Class Test - write a description of the class here
*
* @author (your names)
* @version (a version number)
*/
public class MailProgram extends JFrame implements MouseListener,ActionListener, MouseMotionListener
{
// instance variables - replace the example below with your own
private int x;
//We have 3 swing components that we must declare up here, so every method has access to them
JTextField textField = new JTextField(20);
JButton lowerCase= new JButton ("To Lower");
JButton upperCase = new JButton ("To Upper");
/**
* Called by the browser or applet viewer to inform this JApplet that it
* has been loaded into the system. It is always called before the first
* time that the start method is called.
*/
public void init()
{
Container screen = getContentPane();
screen.setBackground(Color.green);
screen.setLayout (new FlowLayout() );
screen.add(textField);
screen.add(lowerCase);
screen.add(upperCase);
lowerCase.addActionListener(this);
upperCase.addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
/**
* Paint method for applet.
*
* @param g the Graphics object for this applet
*/
public void paint(Graphics g)
{
super.paint(g);
}
public void actionPerformed(ActionEvent thisEvent)
{
Object source = thisEvent.getSource();
if (source == lowerCase)
{
String text=textField.getText();
String lower=text.toLowerCase();
textField.setText(lower);
}
if (source == upperCase)
{
String text=textField.getText();
String caps=text.toUpperCase();
textField.setText(caps);
}
}
//when someone presses the mouse button
public void mousePressed(MouseEvent e)
{
}
//when someone releases the mouse button
public void mouseReleased(MouseEvent e)
{
}
// when the mouse enters the applet
public void mouseEntered(MouseEvent e)
{
}
//when the mouse leaves the applet
public void mouseExited(MouseEvent e)
{
}
//when the mouse button is clicked
public void mouseClicked(MouseEvent e)
{
}
//the mouse button is pressed and the mouse makes a significantly large movement
public void mouseDragged(MouseEvent e)
{
}
//the mouse makes a significantly large movement
public void mouseMoved(MouseEvent e)
{
int x=e.getX();
int y=e.getY();
}
public void sendMail(String to, String subject, String body){
try{
final String SMTP_HOST_NAME = "smtp.gmail.com";
final int SMTP_HOST_PORT = 465;
final String SMTP_AUTH_USER = "deeringCS@gmail.com";
final String SMTP_AUTH_PWD = "mrborland";
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", SMTP_HOST_NAME);
props.put("mail.smtps.auth", "true");
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject(subject);
message.setContent(body, "text/plain");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
transport.connect
(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
catch (Exception e){}
}
public void sendMail(String to, String subject, String body, File file){
try{
final String SMTP_HOST_NAME = "smtp.gmail.com";
final int SMTP_HOST_PORT = 465;
final String SMTP_AUTH_USER = "deeringCS@gmail.com";
final String SMTP_AUTH_PWD = "mrborland";
Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", SMTP_HOST_NAME);
props.put("mail.smtps.auth", "true");
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
/**
* multipart attachments here, part one is the message text,
* the other is the actual file. notice the explicit mime type
* declarations
*
*/
MimeMessage message = new MimeMessage(mailSession);
message.setSubject(subject);
Multipart multiPart = new MimeMultipart();
MimeBodyPart messageText = new MimeBodyPart();
messageText.setContent(body, "text/html; charset=UTF-8");
multiPart.addBodyPart(messageText);
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.attachFile(file);
messageBodyPart.setHeader("Content-Type", "text/plain; charset=\"us-ascii\"; name=\""+file.getName()+"\"");
multiPart.addBodyPart(messageBodyPart);
message.setContent(multiPart);
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
transport.connect (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
public static void main(String[] args)
{
MailProgram m = new MailProgram();
m.setSize(520,520);
//m.setExtendedState (JFrame.MAXIMIZED_BOTH);
m.setVisible(true);
m.init();
}
}
Do one extension:
- Add a way for user to put in their username/password.
- add the ability to send an attachment.
- Add the ability to send their signature (to do this, you will need buffergraphics and mouse dragged and send that as an attachment. )
- Break up a short message in to many messages (like if the message is I LOVE YOU it would send each letter as a message.)
- Anything else - just ask me
submit online, with both names on it. Submit for each user.
Additional Gmail account:
user:deeringcs2@gmail.com
password: mrborland
|