1 import javax.activation.CommandMap;
   2 import javax.activation.MailcapCommandMap;
   3 import javax.mail.BodyPart;
   4 import javax.mail.Message;
   5 import javax.mail.MessagingException;
   6 import javax.mail.Multipart;
   7 import javax.mail.Session;
   8 import javax.mail.internet.InternetAddress;
   9 import javax.mail.internet.MimeBodyPart;
  10 import javax.mail.internet.MimeMessage;
  11 import javax.mail.internet.MimeMultipart;
  12 import javax.xml.soap.AttachmentPart;
  13 import javax.xml.soap.MessageFactory;
  14 import javax.xml.soap.SOAPException;
  15 import javax.xml.soap.SOAPMessage;
  16 import java.io.ByteArrayOutputStream;
  17 import java.io.IOException;
  18 import java.util.Properties;
  19 
  20 /*
  21  * @test
  22  * @bug 8043129
  23  * @summary JAF initialisation in SAAJ clashing with the one in javax.mail
  24  * @author mkos
  25  * @library javax.mail.jar
  26  * @build MailTest
  27  * @run main MailTest
  28  */
  29 public class MailTest {
  30 
  31     String host = null;
  32     String user = "";
  33     String password = null;
  34     String from = null;
  35     String to = null;
  36 
  37     public static void main(String[] args) {
  38         MailTest t = new MailTest();
  39 
  40         t.user = "somebody@somewhere.com";
  41         t.from = "somebody@somewhere.com";
  42         t.to = "somebody@somewhere.com";
  43 
  44         t.user = "somebody@somewhere.com";
  45         t.password = "somepassword";
  46         t.host = "somehost";
  47 
  48         t.sendMail();    //this works
  49 
  50         t.addSoapAttachement();
  51         t.sendMail();    //after addAttachmentPart to soapmessage it do not work
  52 
  53         // workaroundJAFSetup();
  54         // t.sendMail();    //after workaround works again
  55     }
  56 
  57     void addSoapAttachement() {
  58         try {
  59             MessageFactory messageFactory = MessageFactory.newInstance();
  60             SOAPMessage message = messageFactory.createMessage();
  61             AttachmentPart a = message.createAttachmentPart();
  62             a.setContentType("binary/octet-stream");
  63             message.addAttachmentPart(a);
  64         } catch (SOAPException e) {
  65             e.printStackTrace();
  66         }
  67     }
  68 
  69     void sendMail() {
  70 
  71         try {
  72             Properties props = new Properties();
  73             props.put("mail.smtp.host", host);
  74             props.put("mail.smtp.auth", "true");
  75 
  76             Session session = Session.getInstance(props);
  77             session.setDebug(true);
  78 
  79             // Define message
  80             MimeMessage message = new MimeMessage(session);
  81             message.setFrom(new InternetAddress(from));
  82             message.addRecipients(Message.RecipientType.TO, to);
  83             message.setSubject("this is a multipart test");
  84 
  85             Multipart multipart = new MimeMultipart();
  86 
  87             BodyPart messageBodyPart1 = new MimeBodyPart();
  88             messageBodyPart1.setText("please send also this Content\n ciao!");
  89             multipart.addBodyPart(messageBodyPart1);
  90 
  91             BodyPart messageBodyPart2 = new MimeBodyPart();
  92             messageBodyPart2.setContent("<b>please</b> send also this Content <br>ciao!", "text/html; charset=UTF-8");
  93             multipart.addBodyPart(messageBodyPart2);
  94 
  95             message.setContent(multipart);
  96            
  97             /*
  98             Transport tr = session.getTransport("smtp");
  99             tr.connect(host,user, password);
 100             tr.sendMessage(message,InternetAddress.parse(to));
 101             tr.close();
 102             */
 103             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 104             message.writeTo(baos);
 105             String output = baos.toString();
 106             System.out.println("output = " + output);
 107             if (output.contains("also this Content")) {
 108                 System.out.println("Test PASSED.");
 109             } else {
 110                 System.out.println("Test FAILED, missing content.");
 111                 throw new IllegalStateException("Test FAILED, missing content.");
 112             }
 113         } catch (MessagingException ignored) {
 114         } catch (IOException ignored) {
 115         }
 116     }
 117 
 118     // this is how the error can be worked around ...
 119     static void workaroundJAFSetup() {
 120         MailcapCommandMap mailMap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
 121         mailMap.addMailcap("multipart/mixed;;x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
 122     }
 123 }