email me at borlaj@portlandschools.org

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

Sending Mail With Attachments

 

To send an email with an attachment use the code below:

  • The JAR file that you need to use is here. Save it in the same directory that you are working in.
  • In BLUEJ - click preferences - Add library and find that jar file.

Have these imports:

import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import java.io.*;
import javax.activation.*;
import javax.imageio.ImageIO; 

 

Now add the code below as a method and call it. Replace

   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);
        }
    }
                
Now to call it, you need a file. It could be one that the user chooses w/ filechooser, or one that you find on the file system, or it could be created from buffergraphics.
 
    //     From bufferghics:
    //     File outputfile = new File("paintbrush.png");
    //     ImageIO.write(bufferedImage, "png", outputfile);
    // 
    //     from a file chooser
    //     JFileChooser fc=new JFileChooser();
    //     fc.showOpenDialog(null);       
    //     File myFile=fc.getSelectedFile();
    // 
    //     from a file you know
    //     File myFile=new File("Square.java");
    // 
    //     sendMail("jeff@myonlinegrades.com","hi","sdfsdfsf",myFile);