// Collection of code snippets by Arne VajhÃ¸j 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;
import java.util.*;

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

public class TestMemoryDataSource {
   private final static String SMTP_SERVER = "192.168.1.10";
   private final static String FROM = "arne@vajhoej.dk";
   public static void send(String to, String subj, String body) {
      try {
         Properties props = System.getProperties();
         props.put("mail.smtp.host", SMTP_SERVER);
         Session session = Session.getDefaultInstance(props, null);
         MimeMessage message = new MimeMessage(session);
         message.setFrom(new InternetAddress(FROM));
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
         message.setSubject(subj, "ISO-8859-1");
         MimeBodyPart mpart1 = new MimeBodyPart();
         mpart1.setText(body, "ISO-8859-1");
         MimeBodyPart mpart2 = new MimeBodyPart();
         mpart2.setDataHandler(new DataHandler(new MemoryDataSource("Stor tekst")));
         mpart2.setFileName("pseudo.txt");
         MimeMultipart mpart = new MimeMultipart();
         mpart.addBodyPart(mpart1);
         mpart.addBodyPart(mpart2);
         message.setContent(mpart);
         Transport.send(message);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   public static void main(String[] args) {
      send("arne_v@mail.danbbs.dk", "Test", "Vedhæftet pseudo.txt\n");
   }
}

class MemoryDataSource implements DataSource {
   private String data;
   public MemoryDataSource(String data) {
      this.data = data;
   }
   public String getContentType() {
      return "text/plain";
   }
   public InputStream getInputStream() throws IOException {
      return new ByteArrayInputStream(data.getBytes());
   }
   public String getName() {
      return "pseudo.txt";
   }
   public OutputStream getOutputStream() throws IOException {
      return null;
   }
}
