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 15, 2024
1 parent ea55e3e commit 339e3ab
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 184 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.enonic.xp.mail;

import java.util.Map;
import java.util.Objects;

import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteSource;

import com.enonic.xp.annotation.PublicApi;
import com.enonic.xp.util.MediaTypes;

@PublicApi
public final class MailAttachment
{
private final String fileName;

private final ByteSource data;

private final String mimeType;

private final Map<String, String> headers;

private MailAttachment( final Builder builder )
{
this.fileName = Objects.requireNonNull( builder.fileName );
this.data = Objects.requireNonNull( builder.data );
this.mimeType = builder.mimeType != null ? builder.mimeType : resolveMimeType( builder.fileName );
this.headers = builder.headers != null ? ImmutableMap.copyOf( builder.headers ) : ImmutableMap.of();
}

private String resolveMimeType( final String fileName )
{
return MediaTypes.instance().fromFile( fileName ).toString();
}

public static Builder create()
{
return new Builder();
}

public String getFileName()
{
return fileName;
}

public ByteSource getData()
{
return data;
}

public String getMimeType()
{
return mimeType;
}

public Map<String, String> getHeaders()
{
return headers;
}

public static class Builder
{
private String fileName;

private ByteSource data;

private String mimeType;

private Map<String, String> headers;

public Builder fileName( final String fileName )
{
this.fileName = fileName;
return this;
}

public Builder data( final ByteSource data )
{
this.data = data;
return this;
}

public Builder mimeType( final String mimeType )
{
this.mimeType = mimeType;
return this;
}

public Builder headers( final Map<String, String> headers )
{
this.headers = headers;
return this;
}

public MailAttachment build()
{
return new MailAttachment( this );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.enonic.xp.mail;

import com.enonic.xp.annotation.PublicApi;

@PublicApi
public final class MailHeader
{
private final String key;

private final String value;

public MailHeader( final String key, final String value )
{
this.key = key;
this.value = value;
}

public static MailHeader from( final String key, final String value )
{
return new MailHeader( key, value );
}

public String getKey()
{
return key;
}

public String getValue()
{
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import javax.mail.internet.MimeMessage;

import com.enonic.xp.annotation.PublicApi;

@PublicApi
@Deprecated
public interface MailMessage
{
@Deprecated
void compose( MimeMessage message )
throws Exception;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.enonic.xp.mail;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.enonic.xp.annotation.PublicApi;

@PublicApi
public class SendMailParams
public final class SendMailParams
{
private final List<String> to;

Expand All @@ -20,28 +20,28 @@ public class SendMailParams

private final List<String> replyTo;

private final List<MailHeader> headers;

private final List<MailAttachment> attachments;

private final String subject;

private final String contentType;

private final String body;

private final Map<String, String> headers;

private final List<Map<String, Object>> attachments;

private SendMailParams( Builder builder )
{
this.to = builder.to;
this.from = builder.from;
this.cc = builder.cc;
this.bcc = builder.bcc;
this.replyTo = builder.replyTo;
this.to = List.copyOf( builder.to );
this.from = List.copyOf( builder.from );
this.cc = List.copyOf( builder.cc );
this.bcc = List.copyOf( builder.bcc );
this.replyTo = List.copyOf( builder.replyTo );
this.headers = List.copyOf( builder.headers );
this.attachments = List.copyOf( builder.attachments );
this.subject = builder.subject;
this.contentType = builder.contentType;
this.body = builder.body;
this.headers = builder.headers;
this.attachments = builder.attachments;
}

public static Builder create()
Expand Down Expand Up @@ -89,12 +89,21 @@ public String getBody()
return body;
}

public Map<String, String> getHeaders()
public List<MailHeader> getHeaders()
{
return headers;
}

public List<Map<String, Object>> getAttachments()
public String getHeader( String header )
{
return headers.stream()
.filter( h -> h.getKey().equalsIgnoreCase( header ) )
.map( MailHeader::getValue )
.findFirst()
.orElse( null );
}

public List<MailAttachment> getAttachments()
{
return attachments;
}
Expand All @@ -111,71 +120,71 @@ public static class Builder

private final List<String> replyTo = new ArrayList<>();

private final List<MailHeader> headers = new ArrayList<>();

private final List<MailAttachment> attachments = new ArrayList<>();

private String subject;

private String contentType;

private String body;

private Map<String, String> headers;

private List<Map<String, Object>> attachments;

public Builder to( final String... to )
{
this.to.addAll( Arrays.asList( to ) );
this.to.addAll( List.of( to ) );
return this;
}

public Builder to( final List<String> to )
public Builder to( final Collection<String> to )
{
this.to.addAll( to );
return this;
}

public Builder from( final String... from )
{
this.from.addAll( Arrays.asList( from ) );
this.from.addAll( List.of( from ) );
return this;
}

public Builder from( final List<String> from )
public Builder from( final Collection<String> from )
{
this.from.addAll( from );
return this;
}

public Builder cc( final String... cc )
{
this.cc.addAll( Arrays.asList( cc ) );
this.cc.addAll( List.of( cc ) );
return this;
}

public Builder cc( final List<String> cc )
public Builder cc( final Collection<String> cc )
{
this.cc.addAll( cc );
return this;
}

public Builder bcc( final String... bcc )
{
this.bcc.addAll( Arrays.asList( bcc ) );
this.bcc.addAll( List.of( bcc ) );
return this;
}

public Builder bcc( final List<String> bcc )
public Builder bcc( final Collection<String> bcc )
{
this.bcc.addAll( bcc );
return this;
}

public Builder replyTo( final String... replyTo )
{
this.replyTo.addAll( Arrays.asList( replyTo ) );
this.replyTo.addAll( List.of( replyTo ) );
return this;
}

public Builder replyTo( final List<String> replyTo )
public Builder replyTo( final Collection<String> replyTo )
{
this.replyTo.addAll( replyTo );
return this;
Expand All @@ -199,15 +208,26 @@ public Builder body( final String body )
return this;
}

public Builder headers( final Map<String, String> headers )
public Builder addHeader( final String key, final String value )
{
this.headers.add( MailHeader.from( key, value ) );
return this;
}

public void addHeaders( final Map<String, String> headers )
{
headers.forEach( this::addHeader );
}

public Builder addAttachment( final MailAttachment attachment )
{
this.headers = headers;
this.attachments.add( attachment );
return this;
}

public Builder attachments( final List<Map<String, Object>> attachments )
public Builder addAttachments( final Collection<MailAttachment> attachments )
{
this.attachments = attachments;
this.attachments.addAll( attachments );
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import com.enonic.xp.core.internal.concurrent.SimpleExecutor;
import com.enonic.xp.mail.MailException;
import com.enonic.xp.mail.MailMessage;
import com.enonic.xp.mail.SendMailParams;
import com.enonic.xp.mail.MailService;
import com.enonic.xp.mail.SendMailParams;

@Component(immediate = true, configurationPid = "com.enonic.xp.mail")
public final class MailServiceImpl
Expand All @@ -34,7 +34,7 @@ public final class MailServiceImpl

private Session session;

private String defaultFromEmail;
private volatile String defaultFromEmail;

public MailServiceImpl()
{
Expand Down

0 comments on commit 339e3ab

Please sign in to comment.