1+ /*
2+ * Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved.
3+ *
4+ * This program and the accompanying materials are made available under the
5+ * terms of the Eclipse Public License v. 2.0, which is available at
6+ * http://www.eclipse.org/legal/epl-2.0.
7+ *
8+ * This Source Code may also be made available under the following Secondary
9+ * Licenses when the conditions for such availability set forth in the
10+ * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+ * version 2 with the GNU Classpath Exception, which is available at
12+ * https://www.gnu.org/software/classpath/license.html.
13+ *
14+ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+ */
16+
17+ package com .sun .mail .test ;
18+
19+ import static org .junit .Assert .assertEquals ;
20+ import static org .junit .Assert .fail ;
21+
22+ import java .io .IOException ;
23+ import java .net .InetSocketAddress ;
24+ import java .net .Socket ;
25+ import java .net .UnknownHostException ;
26+ import java .util .Properties ;
27+ import java .util .concurrent .ExecutorService ;
28+ import java .util .concurrent .Executors ;
29+ import java .util .concurrent .TimeUnit ;
30+
31+ import javax .mail .Authenticator ;
32+ import javax .mail .MessagingException ;
33+ import javax .mail .PasswordAuthentication ;
34+ import javax .mail .Session ;
35+ import javax .mail .Transport ;
36+ import javax .mail .internet .InternetAddress ;
37+ import javax .mail .internet .MimeMessage ;
38+
39+ import org .junit .Test ;
40+
41+
42+ public class Issue299Test {
43+
44+ static final int PORT = 18465 ;
45+ static final String USER = "xxxunj******@163.com" ;
46+ static final String PASSWD = "EY**************" ;
47+ static final String toMail = "甲申申甶甴甸电甹甸甸畀畱畱瘮畣畯畭甾瘍瘊畄畁畔畁瘍瘊畓畵畢番略畣畴町畐畗畎畅畄瘍瘊瘍瘊畉瘠界畏畖畅瘠留畏畕瘡瘍瘊瘮瘍瘊畑畕畉畔瘍瘊@qq.com" ;
48+ static String TEXT = "Hello world" ;
49+ static String SUBJECT = "Test" ;
50+
51+ static Properties properties = new Properties ();
52+ static Authenticator authenticator = null ;
53+
54+ public static void initProp (){
55+ properties .setProperty ("mail.host" ,"127.0.0.1" );
56+ properties .put ("mail.debug" , "true" );
57+ properties .setProperty ("mail.transport.protocol" ,"smtp" );
58+ properties .setProperty ("mail.smtp.auth" ,"true" );
59+ properties .setProperty ("mail.smtp.timeout" , "1000" );
60+ properties .setProperty ("mail.smtp.port" , Integer .toString (PORT ));
61+ authenticator = new Authenticator () {
62+ @ Override
63+ protected PasswordAuthentication getPasswordAuthentication () {
64+ return new PasswordAuthentication (USER , PASSWD );
65+ }
66+ };
67+ }
68+ public static Session getSession (){
69+ return Session .getInstance (properties , authenticator );
70+ }
71+
72+ @ Test
73+ public void test () throws Exception {
74+ FakeSMTPServer server = new FakeSMTPServer ();
75+ ExecutorService service = Executors .newFixedThreadPool (1 );
76+ try {
77+ service .execute (() -> {
78+ try {
79+ server .start (PORT );
80+ Thread .sleep (1000 );
81+ } catch (Exception e ) {
82+ e .printStackTrace ();
83+ }
84+ });
85+
86+ initProp ();
87+
88+ Session session = getSession ();
89+ MimeMessage msg = new MimeMessage (session );
90+ msg .setRecipient (MimeMessage .RecipientType .TO , new InternetAddress (toMail ));
91+ msg .setSubject (SUBJECT );
92+ msg .setContent (TEXT , "text/html;charset=utf-8" );
93+ msg .saveChanges ();
94+ Transport .send (msg );
95+ fail ("It is expected an IllegalArgumentException because there are illegal characters in the command" );
96+ } catch (Exception e ) {
97+ if (e .getCause () == null || e .getCause ().getClass () != IllegalArgumentException .class ) {
98+ fail ("It is expected an IllegalArgumentException because there are illegal characters in the command. Exception was: " + e );
99+ }
100+ } finally {
101+ server .stop ();
102+ service .shutdown ();
103+ service .awaitTermination (5 , TimeUnit .SECONDS );
104+ }
105+ }
106+
107+ }
0 commit comments