1

I want to send an email with an image attached with it. I am using spring 3 with velocity templates. I am able to do that but for some reasons when I add an extension with the image name I don't get the email delivered.

Following is the code I am using for it:

private MimeMessage createEmail(Application application, String templatePath,   String subject, String toEmail, String fromEmail, String fromName) {
    MimeMessage mimeMsg = mailSender.createMimeMessage();
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("application", application);
    String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templatePath, model);
    text = text.replaceAll("\n", "<br>");

    try {

        MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true);
        helper.setSubject(subject);
        helper.setTo(toEmail);

        if (fromName == null) {
            helper.setFrom(fromEmail);
        } else {
            try {
                helper.setFrom(fromEmail, fromName);
            } catch (UnsupportedEncodingException e) {
                helper.setFrom(fromEmail);
            }
        }

        helper.setSentDate(application.getDateCreated());
        helper.setText(text, true);

        InputStream inputStream = servletContext.getResourceAsStream("images/formstack1.jpg");
        helper.addAttachment("formstack1",  new ByteArrayResource(IOUtils.toByteArray(inputStream)));


    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
    catch (IOException e) {
        throw new RuntimeException(e);
    }

    return mimeMsg;
}

Using the code above I could add formstack1 as attachment but it has no extension so I don't get the formstack1.jpg image file. But when I use formstack1.jpg for the name of resource to be attached in helper.addAttachment("formstack1", new ByteArrayResource(IOUtils.toByteArray(inputStream))); as formstack1 changed to formstack1.jpg I don't get even the email delivered. I am using smtp.gmail.com and 25 for port. I do get the email sent successfully message on the console though. But the email is never delivered.

EDIT: If I keep it like helper.addAttachment("formstack1", new ByteArrayResource(IOUtils.toByteArray(inputStream))); and change the extension from nothing to .jpg while downloading the attached image I do get the desired image.

Could someone help me understand why is it happening and how send email with 1 or more attachments using spring 3.

Thanks.

1 Answer 1

2

You should better use Apache Commons HtmlEMail

http://commons.apache.org/email/userguide.html

Sign up to request clarification or add additional context in comments.

2 Comments

Can't get the commons-email-1.3 version and the maven repository has only commons-email-1.1 as latest. Am I missing something? apache.techartifact.com/mirror//commons/email/binaries/… is the download link for that.
Looks like .jpg images don't fit into the spec even after setting the content type as image/jpeg it didn't work. But when I tried a .png everything worked alright.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.