Sending an Email with an Attachment Using JavaMail

1. Follow the steps in the previous post titled “Sending an Email from Java Using JavaMail and Apache James”, but replace the contents of the EmailTest.java file with the text below. The updated code attaches a copy of the source code to the outgoing mail message.

import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;

public class EmailTest
{
    public static void main(String[] args)
    {
        String addressee = args[0];        

        EmailMessage emailMessage = new EmailMessage
        (
            "nobody@null.net",
            addressee,
            "test email",
            "EmailTest.java",
            "This is a test email."
        );

        emailMessage.send();

        System.out.println("email message sent to " + addressee);
    }
}

class EmailMessage
{
    public String sender;
    public String addressee;
    public String subject;
    public String nameOfAttachedFile;
    public String body;

    public EmailMessage
    (
        String sender,
        String addressee,
        String subject,
        String nameOfAttachedFile,
        String body
    )
    {
        this.sender = sender;
        this.addressee = addressee;
        this.subject = subject;
        this.nameOfAttachedFile = nameOfAttachedFile;
        this.body = body;
    }

    public void send()
    {
        Properties mailServerProperties = new Properties();
        mailServerProperties.put
        (
            "mail.smtp.host",
            "localhost"
        );
        mailServerProperties.put
        (
            "mail.smtp.port",
            "25"
        );

            Session session = Session.getDefaultInstance
        (
            mailServerProperties
        );

        MimeMessage messageToSend = new MimeMessage(session);

        try
        {
            messageToSend.setFrom
            (
                new InternetAddress(this.sender)
            );
            messageToSend.addRecipient
            (
                Message.RecipientType.TO,
                new InternetAddress(this.addressee)
            );
            messageToSend.setSubject(this.subject);

            BodyPart bodyText = new MimeBodyPart();
            bodyText.setText(this.body);

            BodyPart attachment = new MimeBodyPart();
            DataSource dataSource = new FileDataSource
            (
                this.nameOfAttachedFile
            );
            attachment.setDataHandler(new DataHandler(dataSource));
            attachment.setFileName(this.nameOfAttachedFile);

            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(bodyText);
            multipart.addBodyPart(attachment);

            messageToSend.setContent(multipart);

            Transport.send(messageToSend);
        }
        catch (MessagingException ex)
        {
            ex.printStackTrace();
        }
    }
}
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s