Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/ws/8043129/MailTest.java
38853 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 804312926* @summary JAF initialisation in SAAJ clashing with the one in javax.mail27* @author mkos28* @library javax.mail.jar29* @build MailTest30* @run main MailTest31*/3233import javax.activation.CommandMap;34import javax.activation.MailcapCommandMap;35import javax.mail.BodyPart;36import javax.mail.Message;37import javax.mail.MessagingException;38import javax.mail.Multipart;39import javax.mail.Session;40import javax.mail.internet.InternetAddress;41import javax.mail.internet.MimeBodyPart;42import javax.mail.internet.MimeMessage;43import javax.mail.internet.MimeMultipart;44import javax.xml.soap.AttachmentPart;45import javax.xml.soap.MessageFactory;46import javax.xml.soap.SOAPException;47import javax.xml.soap.SOAPMessage;48import java.io.ByteArrayOutputStream;49import java.io.IOException;50import java.util.Properties;5152public class MailTest {5354String host = null;55String user = "";56String password = null;57String from = null;58String to = null;5960public static void main(String[] args) {61MailTest t = new MailTest();6263t.user = "[email protected]";64t.from = "[email protected]";65t.to = "[email protected]";6667t.user = "[email protected]";68t.password = "somepassword";69t.host = "somehost";7071t.sendMail(); //this works7273t.addSoapAttachement();74t.sendMail(); //after addAttachmentPart to soapmessage it do not work7576// workaroundJAFSetup();77// t.sendMail(); //after workaround works again78}7980void addSoapAttachement() {81try {82MessageFactory messageFactory = MessageFactory.newInstance();83SOAPMessage message = messageFactory.createMessage();84AttachmentPart a = message.createAttachmentPart();85a.setContentType("binary/octet-stream");86message.addAttachmentPart(a);87} catch (SOAPException e) {88e.printStackTrace();89}90}9192void sendMail() {9394try {95Properties props = new Properties();96props.put("mail.smtp.host", host);97props.put("mail.smtp.auth", "true");9899Session session = Session.getInstance(props);100session.setDebug(true);101102// Define message103MimeMessage message = new MimeMessage(session);104message.setFrom(new InternetAddress(from));105message.addRecipients(Message.RecipientType.TO, to);106message.setSubject("this is a multipart test");107108Multipart multipart = new MimeMultipart();109110BodyPart messageBodyPart1 = new MimeBodyPart();111messageBodyPart1.setText("please send also this Content\n ciao!");112multipart.addBodyPart(messageBodyPart1);113114BodyPart messageBodyPart2 = new MimeBodyPart();115messageBodyPart2.setContent("<b>please</b> send also this Content <br>ciao!", "text/html; charset=UTF-8");116multipart.addBodyPart(messageBodyPart2);117118message.setContent(multipart);119120/*121Transport tr = session.getTransport("smtp");122tr.connect(host,user, password);123tr.sendMessage(message,InternetAddress.parse(to));124tr.close();125*/126127ByteArrayOutputStream baos = new ByteArrayOutputStream();128message.writeTo(baos);129String output = baos.toString();130System.out.println("output = " + output);131if (output.contains("also this Content")) {132System.out.println("Test PASSED.");133} else {134System.out.println("Test FAILED, missing content.");135throw new IllegalStateException("Test FAILED, missing content.");136}137} catch (MessagingException ignored) {138} catch (IOException ignored) {139}140}141142// this is how the error can be worked around ...143static void workaroundJAFSetup() {144MailcapCommandMap mailMap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();145mailMap.addMailcap("multipart/mixed;;x-java-content-handler=com.sun.mail.handlers.multipart_mixed");146}147}148149150