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  * @run main MailTest
  32  */
  33 
  34 import javax.activation.CommandMap;
  35 import javax.activation.MailcapCommandMap;
  36 import javax.mail.BodyPart;
  37 import javax.mail.Message;
  38 import javax.mail.MessagingException;
  39 import javax.mail.Multipart;
  40 import javax.mail.Session;
  41 import javax.mail.internet.InternetAddress;
  42 import javax.mail.internet.MimeBodyPart;
  43 import javax.mail.internet.MimeMessage;
  44 import javax.mail.internet.MimeMultipart;
  45 import javax.xml.soap.AttachmentPart;
  46 import javax.xml.soap.MessageFactory;
  47 import javax.xml.soap.SOAPException;
  48 import javax.xml.soap.SOAPMessage;
  49 import java.io.ByteArrayOutputStream;
  50 import java.io.IOException;
  51 import java.util.Properties;
  52 
  53 public class MailTest {
  54 
  55     String host = null;
  56     String user = "";
  57     String password = null;
  58     String from = null;
  59     String to = null;
  60 
  61     public static void main(String[] args) {
  62         MailTest t = new MailTest();
  63 
  64         t.user = "somebody@somewhere.com";
  65         t.from = "somebody@somewhere.com";
  66         t.to = "somebody@somewhere.com";
  67 
  68         t.user = "somebody@somewhere.com";
  69         t.password = "somepassword";
  70         t.host = "somehost";
  71 
  72         t.sendMail();    //this works
  73 
  74         t.addSoapAttachement();
  75         t.sendMail();    //after addAttachmentPart to soapmessage it do not work
  76 
  77         // workaroundJAFSetup();
  78         // t.sendMail();    //after workaround works again
  79     }
  80 
  81     void addSoapAttachement() {
  82         try {
  83             MessageFactory messageFactory = MessageFactory.newInstance();
  84             SOAPMessage message = messageFactory.createMessage();
  85             AttachmentPart a = message.createAttachmentPart();
  86             a.setContentType("binary/octet-stream");
  87             message.addAttachmentPart(a);
  88         } catch (SOAPException e) {
  89             e.printStackTrace();
  90         }
  91     }
  92 
  93     void sendMail() {
  94 
  95         try {
  96             Properties props = new Properties();
  97             props.put("mail.smtp.host", host);
  98             props.put("mail.smtp.auth", "true");
  99 
 100             Session session = Session.getInstance(props);
 101             session.setDebug(true);
 102 
 103             // Define message
 104             MimeMessage message = new MimeMessage(session);
 105             message.setFrom(new InternetAddress(from));
 106             message.addRecipients(Message.RecipientType.TO, to);
 107             message.setSubject("this is a multipart test");
 108 
 109             Multipart multipart = new MimeMultipart();
 110 
 111             BodyPart messageBodyPart1 = new MimeBodyPart();
 112             messageBodyPart1.setText("please send also this Content\n ciao!");
 113             multipart.addBodyPart(messageBodyPart1);
 114 
 115             BodyPart messageBodyPart2 = new MimeBodyPart();
 116             messageBodyPart2.setContent("<b>please</b> send also this Content <br>ciao!", "text/html; charset=UTF-8");
 117             multipart.addBodyPart(messageBodyPart2);
 118 
 119             message.setContent(multipart);
 120 
 121             /*
 122                 Transport tr = session.getTransport("smtp");
 123                 tr.connect(host,user, password);
 124                 tr.sendMessage(message,InternetAddress.parse(to));
 125                 tr.close();
 126             */
 127 
 128             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 129             message.writeTo(baos);
 130             String output = baos.toString();
 131             System.out.println("output = " + output);
 132             if (output.contains("also this Content")) {
 133                 System.out.println("Test PASSED.");
 134             } else {
 135                 System.out.println("Test FAILED, missing content.");
 136                 throw new IllegalStateException("Test FAILED, missing content.");
 137             }
 138         } catch (MessagingException ignored) {
 139         } catch (IOException ignored) {
 140         }
 141     }
 142 
 143     // this is how the error can be worked around ...
 144     static void workaroundJAFSetup() {
 145         MailcapCommandMap mailMap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
 146         mailMap.addMailcap("multipart/mixed;;x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
 147     }
 148 }