title: How to send email with attachment using InputStream and Spring?
body: <p>The situation is like this:</p>
  
  <p>First, we generate a file in the memory, we can get a <code>InputStream</code> object.
  Second the <em>InputStream</em> object must be send as a attachment of a email. The language is Java, we use Spring to send email.</p>
  
  <p>I have found a lot of information, but I cannot find how to send an email attachment using <code>InputStream</code>. I try to do like this:</p>
  
  <pre><code>InputStreamSource iss= new InputStreamResource(new FileInputStream("c:\\a.txt"));
  MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
  message.addAttachment("attachment", iss);
  </code></pre>
  
  <p>But I get an  exception:</p>
  
  <blockquote>
    <p>Passed-in Resource contains an open stream: invalid argument. JavaMail
    requires an InputStreamSource that creates a fresh stream for every
    call.</p>
  </blockquote>