title: How to send email with attachments
body: <p>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.</p>
  
  <p>Following is the code I am using for it:</p>
  
  <pre><code>private MimeMessage createEmail(Application application, String templatePath,   String subject, String toEmail, String fromEmail, String fromName) {
      MimeMessage mimeMsg = mailSender.createMimeMessage();
      Map&lt;String, Object&gt; model = new HashMap&lt;String, Object&gt;();
      model.put("application", application);
      String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templatePath, model);
      text = text.replaceAll("\n", "&lt;br&gt;");
  
      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;
  }
  </code></pre>
  
  <p>Using the code above I could add <strong>formstack1</strong> as attachment but it has no extension so I don't get the <strong>formstack1.jpg</strong> image file. But when I use <strong>formstack1.jpg</strong> for the name of resource to be attached in <code>helper.addAttachment("formstack1",  new ByteArrayResource(IOUtils.toByteArray(inputStream)));</code> as formstack1 changed to <code>formstack1.jpg</code> I don't get even the email delivered. I am using <code>smtp.gmail.com</code> and <code>25</code> for port. I do get the email sent successfully message on the console though. But the email
  is never delivered.</p>
  
  <p><strong>EDIT:</strong> If I keep it like <code>helper.addAttachment("formstack1",  new ByteArrayResource(IOUtils.toByteArray(inputStream)));</code> and change the extension from nothing to .jpg while downloading the attached image I do get the desired image.</p>
  
  <p>Could someone help me understand why is it happening and how send email with 1 or more attachments using spring 3.</p>
  
  <p>Thanks.</p>