Skip to content

Commit

Permalink
Default email sender is not working
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Mar 14, 2024
1 parent 664d826 commit 782c29e
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
String smtpPassword() default "";

boolean smtpTLS() default false;

String defaultFromEmail();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public final class MailServiceImpl

private Session session;

private String defaultFromEmail;

public MailServiceImpl()
{
this.simpleExecutor = new SimpleExecutor( Executors::newCachedThreadPool, "mail-service-executor-thread-%d",
Expand All @@ -44,6 +46,8 @@ public MailServiceImpl()
@Modified
public void activate( final MailConfig config )
{
this.defaultFromEmail = config.defaultFromEmail();

final Properties properties = new Properties();

properties.put( "mail.transport.protocol", "smtp" );
Expand Down Expand Up @@ -92,7 +96,7 @@ public void send( final MailMessageParams params )
{
try
{
MimeMessage message = MimeMessageConverter.convert( session, params );
MimeMessage message = new MimeMessageConverter( defaultFromEmail, session ).convert( params );
simpleExecutor.execute( () -> {
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import javax.activation.DataHandler;
Expand All @@ -31,14 +33,26 @@

class MimeMessageConverter
{
static MimeMessage convert( Session session, MailMessageParams params )
private static final String DEFAULT_FROM_PATTERN = "<>";

private final String defaultFromEmail;

private final Session session;

MimeMessageConverter( String defaultFromEmail, Session session )
{
this.defaultFromEmail = defaultFromEmail;
this.session = session;
}

MimeMessage convert( MailMessageParams params )
throws Exception
{
MimeMessage message = new MimeMessage( session );

message.setSubject( params.getSubject() );

message.addFrom( toAddresses( params.getFrom() ) );
message.addFrom( toAddresses( resolveFrom( params.getFrom() ) ) );
message.addRecipients( Message.RecipientType.TO, toAddresses( params.getTo() ) );
message.addRecipients( Message.RecipientType.CC, toAddresses( params.getCc() ) );
message.addRecipients( Message.RecipientType.BCC, toAddresses( params.getBcc() ) );
Expand Down Expand Up @@ -91,15 +105,37 @@ static MimeMessage convert( Session session, MailMessageParams params )
return message;
}

private static InternetAddress[] toAddresses( final String[] addressList )
private String[] resolveFrom( final String[] from )
{
return Arrays.stream( from ).filter( Objects::nonNull ).map( sender -> {
if ( sender.contains( DEFAULT_FROM_PATTERN ) )
{
if ( defaultFromEmail == null || defaultFromEmail.isEmpty() )
{
throw new IllegalArgumentException(
String.format( "To use \"%s\" the \"defaultFromEmail\" configuration must be set in \"com.enonic.xp.mail.cfg\"",
DEFAULT_FROM_PATTERN ) );
}
return sender.equals( DEFAULT_FROM_PATTERN )
? defaultFromEmail
: sender.replace( DEFAULT_FROM_PATTERN, String.format( "<%s>", defaultFromEmail ) );
}
else
{
return sender;
}
} ).toArray( String[]::new );
}

private InternetAddress[] toAddresses( final String[] addressList )
{
return Stream.of( addressList )
.filter( string -> !nullToEmpty( string ).isBlank() )
.map( MimeMessageConverter::toAddress )
.map( this::toAddress )
.toArray( InternetAddress[]::new );
}

private static InternetAddress toAddress( final String address )
private InternetAddress toAddress( final String address )
throws MailException
{
try
Expand All @@ -112,7 +148,7 @@ private static InternetAddress toAddress( final String address )
}
}

private static List<Attachment> resolveAttachments( final List<Map<String, Object>> attachments )
private List<Attachment> resolveAttachments( final List<Map<String, Object>> attachments )
{
if ( attachments == null )
{
Expand All @@ -134,7 +170,7 @@ private static List<Attachment> resolveAttachments( final List<Map<String, Objec
return result;
}

private static <T> T getValue( final Map<String, Object> object, final String key, final Class<T> type )
private <T> T getValue( final Map<String, Object> object, final String key, final Class<T> type )
{
final Object value = object.get( key );
if ( type.isInstance( value ) )
Expand All @@ -145,7 +181,7 @@ private static <T> T getValue( final Map<String, Object> object, final String ke
return null;
}

private static String getMimeType( final String fileName )
private String getMimeType( final String fileName )
{
if ( fileName == null )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class MimeMessageConverterTest
Expand All @@ -45,7 +46,7 @@ public void testSimpleMail()
.setHeaders( Map.of( "X-Custom", "Value", "X-Other", "2" ) )
.build();

Message message = MimeMessageConverter.convert( Session.getDefaultInstance( new Properties() ), params );
Message message = new MimeMessageConverter( null, Session.getDefaultInstance( new Properties() ) ).convert( params );

assertEquals( "test subject", message.getSubject() );
assertEquals( "test body", message.getContent() );
Expand All @@ -72,7 +73,7 @@ public void testMultiRecipientsMail()
.setReplyTo( new String[]{"replyTo@bar.com", "replyTo@foo.com"} )
.build();

Message message = MimeMessageConverter.convert( Session.getDefaultInstance( new Properties() ), params );
Message message = new MimeMessageConverter( null, Session.getDefaultInstance( new Properties() ) ).convert( params );

assertEquals( "test subject", message.getSubject() );
assertEquals( "test body", message.getContent() );
Expand All @@ -91,17 +92,58 @@ public void testRfc822AddressMail()
.setSubject( "test subject" )
.setBody( "test body" )
.setTo( new String[]{"To Bar <to@bar.com>", "To Foo <to@foo.com>"} )
.setFrom( new String[]{"From Bar <from@bar.com>", "From Foo <from@foo.com>"} )
.setFrom( new String[]{"From Bar <from@bar.com>", "From Foo <from@foo.com>", "<>", "Some User <>", "username@domain.com"} )
.build();

Message message = MimeMessageConverter.convert( Session.getDefaultInstance( new Properties() ), params );
Message message =
new MimeMessageConverter( "noreply@domain.com", Session.getDefaultInstance( new Properties() ) ).convert( params );

assertEquals( "test subject", message.getSubject() );
assertEquals( "test body", message.getContent() );
assertArrayEquals( toAddresses( "From Bar <from@bar.com>", "From Foo <from@foo.com>" ), message.getFrom() );
assertArrayEquals(
toAddresses( "From Bar <from@bar.com>", "From Foo <from@foo.com>", "noreply@domain.com", "Some User <noreply@domain.com>",
"username@domain.com" ), message.getFrom() );
assertArrayEquals( toAddresses( "To Bar <to@bar.com>", "To Foo <to@foo.com>" ), message.getRecipients( Message.RecipientType.TO ) );
}

@Test
public void testDefaultFromMail() throws Exception
{
MailMessageParams params = MailMessageParams.create()
.setSubject( "test subject" )
.setBody( "test body" )
.setTo( new String[]{"To Bar <to@bar.com>", "To Foo <to@foo.com>"} )
.setFrom( new String[]{"Username <username@domain.com>", "<>", "Some User <>", "username2@domain.com"} )
.build();

Message message =
new MimeMessageConverter( "noreply@domain.com", Session.getDefaultInstance( new Properties() ) ).convert( params );

assertEquals( "test subject", message.getSubject() );
assertEquals( "test body", message.getContent() );
assertArrayEquals(
toAddresses( "Username <username@domain.com>", "noreply@domain.com", "Some User <noreply@domain.com>", "username2@domain.com" ),
message.getFrom() );
assertArrayEquals( toAddresses( "To Bar <to@bar.com>", "To Foo <to@foo.com>" ), message.getRecipients( Message.RecipientType.TO ) );
}

@Test
public void testInvalidDefaultFromMail()
throws Exception
{
MailMessageParams params = MailMessageParams.create()
.setSubject( "test subject" )
.setBody( "test body" )
.setTo( new String[]{"To Bar <to@bar.com>", "To Foo <to@foo.com>"} )
.setFrom( new String[]{"Username <username@domain.com>", "<>", "Some User <>", "username2@domain.com"} )
.build();

IllegalArgumentException ex = assertThrows( IllegalArgumentException.class, () ->
new MimeMessageConverter( null, Session.getDefaultInstance( new Properties() ) ).convert( params ) );

assertEquals( "To use \"<>\" the \"defaultFromEmail\" configuration must be set in \"com.enonic.xp.mail.cfg\"", ex.getMessage() );
}

@Test
public void testSendMailWithContentType()
throws Exception
Expand All @@ -114,7 +156,7 @@ public void testSendMailWithContentType()
.setContentType( "text/html" )
.build();

Message message = MimeMessageConverter.convert( Session.getDefaultInstance( new Properties() ), params );
Message message = new MimeMessageConverter( null, Session.getDefaultInstance( new Properties() ) ).convert( params );

assertEquals( "test subject", message.getSubject() );
assertEquals( "test body", message.getContent() );
Expand Down Expand Up @@ -145,7 +187,7 @@ public void testSendWithAttachments()
.setAttachments( List.of( attachment1, attachment2 ) )
.build();

Message message = MimeMessageConverter.convert( Session.getDefaultInstance( new Properties() ), params );
Message message = new MimeMessageConverter( null, Session.getDefaultInstance( new Properties() ) ).convert( params );
message.saveChanges(); // required to updated headers (mimeType)

MimeMultipart content = (MimeMultipart) message.getContent();
Expand All @@ -172,7 +214,6 @@ public void testSendWithAttachments()
}



private InternetAddress[] toAddresses( final String... addresses )
{
return Stream.of( addresses ).map( this::toAddress ).toArray( InternetAddress[]::new );
Expand Down
3 changes: 2 additions & 1 deletion modules/runtime/src/home/config/com.enonic.xp.mail.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
# smtpAuth=false
# smtpUser=user
# smtpPassword=password
# smtpTLS=false
# smtpTLS=false
# defaultFromEmail=

0 comments on commit 782c29e

Please sign in to comment.