58
58
import org .exist .xmldb .XmldbURI ;
59
59
import org .exist .xquery .XPathException ;
60
60
import org .exist .xquery .value .Sequence ;
61
+ import org .junit .Before ;
61
62
import org .junit .BeforeClass ;
62
63
import org .junit .ClassRule ;
63
64
import org .junit .Rule ;
68
69
69
70
import javax .annotation .Nullable ;
70
71
import java .io .IOException ;
71
- import java .util .EnumSet ;
72
+ import java .util .Arrays ;
72
73
import java .util .Optional ;
73
74
74
75
import static java .nio .charset .StandardCharsets .UTF_8 ;
@@ -90,14 +91,26 @@ enum SmtpImplementation {
90
91
JAKARTA_MAIL
91
92
}
92
93
93
- @ Parameterized . Parameters ( name = "{0}" )
94
- public static java . util . Collection < SmtpImplementation > data () {
95
- return EnumSet . allOf ( SmtpImplementation . class );
94
+ enum AuthenticationOption {
95
+ NOT_AUTHENTICATED ,
96
+ AUTHENTICATED
96
97
}
97
98
98
- @ Parameterized .Parameter
99
+ @ Parameterized .Parameters (name = "{0} {1}" )
100
+ public static java .util .Collection <Object []> data () {
101
+ return Arrays .asList (new Object [][] {
102
+ { SmtpImplementation .SMTP_DIRECT_CONNECTION , AuthenticationOption .NOT_AUTHENTICATED },
103
+ { SmtpImplementation .JAKARTA_MAIL , AuthenticationOption .NOT_AUTHENTICATED },
104
+ { SmtpImplementation .JAKARTA_MAIL , AuthenticationOption .AUTHENTICATED },
105
+ });
106
+ }
107
+
108
+ @ Parameterized .Parameter (0 )
99
109
public SmtpImplementation smtpImplementation ;
100
110
111
+ @ Parameterized .Parameter (1 )
112
+ public AuthenticationOption authenticationOption ;
113
+
101
114
@ ClassRule
102
115
public static final ExistEmbeddedServer existEmbeddedServer = new ExistEmbeddedServer (true , true );
103
116
@@ -107,6 +120,9 @@ public static java.util.Collection<SmtpImplementation> data() {
107
120
private static final XmldbURI BIN_DOC1_NAME = XmldbURI .create ("doc 1.bin" ); // NOTE(AR) intentionally contains a space character to test correct encoding/decoding
108
121
private static final byte [] BIN_DOC1_CONTENT = UUIDGenerator .getUUIDversion4 ().getBytes (UTF_8 );
109
122
123
+ private static final String EMAIL_UID = "emailuid" ;
124
+ private static final String EMAIL_PWD = "emailpwd" ;
125
+
110
126
private final int smtpPort = nextFreePort (2525 , 2599 , 10 );
111
127
112
128
@ Rule
@@ -127,6 +143,13 @@ public static void setup() throws PermissionDeniedException, IOException, SAXExc
127
143
}
128
144
}
129
145
146
+ @ Before
147
+ public void setSmtpAuth () {
148
+ if (authenticationOption == AuthenticationOption .AUTHENTICATED ) {
149
+ greenMail .setUser (EMAIL_UID , EMAIL_PWD );
150
+ }
151
+ }
152
+
130
153
@ Test
131
154
public void sendTextEmail () throws EXistException , XPathException , PermissionDeniedException , IOException , MessagingException {
132
155
final String messageText = UUIDGenerator .getUUIDversion4 ();
@@ -528,6 +551,10 @@ private MimeMessage sendEmail(final String message, @Nullable final String[] att
528
551
}
529
552
530
553
private MimeMessage sendEmailBySmtpDirectConnection (final String message , @ Nullable final String [] attachmentPaths ) throws EXistException , XPathException , PermissionDeniedException , IOException , MessagingException {
554
+ if (authenticationOption == AuthenticationOption .AUTHENTICATED ) {
555
+ throw new UnsupportedOperationException ("Authentication is not yet implemented by SMTP direct connection" );
556
+ }
557
+
531
558
final String from =
"[email protected] " ;
532
559
final String to =
"[email protected] " ;
533
560
final String subject = "some email subject" ;
@@ -601,7 +628,7 @@ private MimeMessage sendEmailByJakartaMail(final String message, final String[]
601
628
tmpAttachmentPaths = "'" + tmpAttachmentPaths + "'" ;
602
629
}
603
630
604
- final String query =
631
+ String query =
605
632
"import module namespace mail = \" http://exist-db.org/xquery/mail\" ;\n " +
606
633
"let $attachments := \n " +
607
634
" for $attachment-path in (" + tmpAttachmentPaths + ")\n " +
@@ -619,8 +646,20 @@ private MimeMessage sendEmailByJakartaMail(final String message, final String[]
619
646
" <properties>\n " +
620
647
" <property name=\" mail.transport.protocol\" value=\" smtp\" />\n " +
621
648
" <property name=\" mail.smtp.port\" value=\" " + smtpPort + "\" />\n " +
622
- " <property name=\" mail.smtp.host\" value=\" 127.0.0.1\" />\n " +
623
- " </properties>\n " +
649
+ " <property name=\" mail.smtp.host\" value=\" 127.0.0.1\" />\n " ;
650
+
651
+ if (authenticationOption == AuthenticationOption .AUTHENTICATED ) {
652
+ query +=
653
+ " <property name=\" mail.smtp.auth\" value=\" true\" />" +
654
+ " </properties>\n " +
655
+ ", \n " +
656
+ "<authentication username='" + EMAIL_UID + "' password='" + EMAIL_PWD + "'/>" ;
657
+ } else {
658
+ query +=
659
+ " </properties>\n " ;
660
+ }
661
+
662
+ query +=
624
663
")\n " +
625
664
" return\n " +
626
665
" mail:send-email(\n " +
0 commit comments