2

Trying to get Play! Framework to send an email with an attachment. Code below works fine if I don't add the attachment to the message. I've tried both with Play's Mailer class and with the Apache Commons classes (as below), but in both cases, the page just sits there with a spinner (Chrome) and no email is received.

EmailAttachment attachment = new EmailAttachment();
attachment.setURL(new URL(base + "public/images/triangles.png"));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("test");
attachment.setName("test");

emailaddress = "[email protected]";

MultiPartEmail email = new MultiPartEmail();
email.setDebug(true);
email.addTo(emailaddress);
email.setFrom("Testing <[email protected]>");
email.setSubject("Testing email");
try
{
    email.attach(attachment);
}
catch (EmailException ex)
{
    System.out.println(ex.getMessage());
}
email.setMsg("test email");
email.send();

1 Answer 1

6

Im guessing you've already had a look at the Examples for Apache Commons and Sending e-mail - Play! Framework 1.1?

IMO I'd suggest using a well known library with loads of documentation and examples like JavaMail and their api.

Here are a few tutorials that will get you started immediately:

An example of using the JavaMail to send a Email with attachment via gmail is:

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailTLS {

public static void main(String[] args) {

    final String username = "[email protected]";
    final String password = "password";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    try {

        // Define message
        MimeMessage message =
                new MimeMessage(session);
        message.setFrom(
                new InternetAddress(from));
        message.addRecipient(
                Message.RecipientType.TO,
                new InternetAddress(to));
        message.setSubject(
                "Hello JavaMail Attachment");

        // create the message part 
        MimeBodyPart messageBodyPart =
                new MimeBodyPart();

        //fill message
        messageBodyPart.setText("Hi");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        // Part two is attachment
        messageBodyPart = new MimeBodyPart();
        DataSource source =
                new FileDataSource(fileAttachment);
        messageBodyPart.setDataHandler(
                new DataHandler(source));
        messageBodyPart.setFileName(fileAttachment);
        multipart.addBodyPart(messageBodyPart);

        // Put parts in message
        message.setContent(multipart);

        // Send the message
        Transport.send(message);


    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

HTH

References:

1
  • Issue is turning out to be a problem in generating the attachment correctly in the first place - it works fine from a browser or cURL request, just not when trying to pull it into a bytearray. Since your answer is helpful and accurate for the original question, I'm going to mark it accepted.
    – Rob Cowell
    Aug 9, 2012 at 15:23

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.