email me at borlaj@portlandschools.org

Loading
notes previous (12/<12) submit the dump links  
 

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 ActionListener
{
    // 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);

      
    }

    /**
     * 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);
        }
    }

   

    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