1 /*
   2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8043129
  27  * @summary JAF initialisation in SAAJ clashing with the one in javax.mail
  28  * @author mkos
  29  * @library javax.mail.jar
  30  * @modules java.xml.ws
  31  * @compile -addmods java.xml.ws MailTest.java
  32  * @run main/othervm -addmods java.xml.ws MailTest
  33  */
  34 
  35 import javax.activation.CommandMap;
  36 import javax.activation.MailcapCommandMap;
  37 import javax.mail.BodyPart;
  38 import javax.mail.Message;
  39 import javax.mail.MessagingException;
  40 import javax.mail.Multipart;
  41 import javax.mail.Session;
  42 import javax.mail.internet.InternetAddress;
  43 import javax.mail.internet.MimeBodyPart;
  44 import javax.mail.internet.MimeMessage;
  45 import javax.mail.internet.MimeMultipart;
  46 import javax.xml.soap.AttachmentPart;
  47 import javax.xml.soap.MessageFactory;
  48 import javax.xml.soap.SOAPException;
  49 import javax.xml.soap.SOAPMessage;
  50 import java.io.ByteArrayOutputStream;
  51 import java.io.IOException;
  52 import java.util.Properties;
  53 
  54 public class MailTest {
  55 
  56     String host = null;
  57     String user = "";
  58     String password = null;
  59     String from = null;
  60     String to = null;
  61 
  62     public static void main(String[] args) {
  63         MailTest t = new MailTest();
  64 
  65         t.user = "somebody@somewhere.com";
  66         t.from = "somebody@somewhere.com";
  67         t.to = "somebody@somewhere.com";
  68 
  69         t.user = "somebody@somewhere.com";
  70         t.password = "somepassword";
  71         t.host = "somehost";
  72 
  73         t.sendMail();    //this works
  74 
  75         t.addSoapAttachement();
  76         t.sendMail();    //after addAttachmentPart to soapmessage it do not work
  77 
  78         // workaroundJAFSetup();
  79         // t.sendMail();    //after workaround works again
  80     }
  81 
  82     void addSoapAttachement() {
  83         try {
  84             MessageFactory messageFactory = MessageFactory.newInstance();
  85             SOAPMessage message = messageFactory.createMessage();
  86             AttachmentPart a = message.createAttachmentPart();
  87             a.setContentType("binary/octet-stream");
  88             message.addAttachmentPart(a);
  89         } catch (SOAPException e) {
  90             e.printStackTrace();
  91         }
  92     }
  93 
  94     void sendMail() {
  95 
  96         try {
  97             Properties props = new Properties();
  98             props.put("mail.smtp.host", host);
  99             props.put("mail.smtp.auth", "true");
 100 
 101             Session session = Session.getInstance(props);
 102             session.setDebug(true);
 103 
 104             // Define message
 105             MimeMessage message = new MimeMessage(session);
 106             message.setFrom(new InternetAddress(from));
 107             message.addRecipients(Message.RecipientType.TO, to);
 108             message.setSubject("this is a multipart test");
 109 
 110             Multipart multipart = new MimeMultipart();
 111 
 112             BodyPart messageBodyPart1 = new MimeBodyPart();
 113             messageBodyPart1.setText("please send also this Content\n ciao!");
 114             multipart.addBodyPart(messageBodyPart1);
 115 
 116             BodyPart messageBodyPart2 = new MimeBodyPart();
 117             messageBodyPart2.setContent("<b>please</b> send also this Content <br>ciao!", "text/html; charset=UTF-8");
 118             multipart.addBodyPart(messageBodyPart2);
 119 
 120             message.setContent(multipart);
 121 
 122             /*
 123                 Transport tr = session.getTransport("smtp");
 124                 tr.connect(host,user, password);
 125                 tr.sendMessage(message,InternetAddress.parse(to));
 126                 tr.close();
 127             */
 128 
 129             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 130             message.writeTo(baos);
 131             String output = baos.toString();
 132             System.out.println("output = " + output);
 133             if (output.contains("also this Content")) {
 134                 System.out.println("Test PASSED.");
 135             } else {
 136                 System.out.println("Test FAILED, missing content.");
 137                 throw new IllegalStateException("Test FAILED, missing content.");
 138             }
 139         } catch (MessagingException ignored) {
 140         } catch (IOException ignored) {
 141         }
 142     }
 143 
 144     // this is how the error can be worked around ...
 145     static void workaroundJAFSetup() {
 146         MailcapCommandMap mailMap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
 147         mailMap.addMailcap("multipart/mixed;;x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
 148     }
 149 }