Skip to content

Commit

Permalink
Default email sender is not working #10404
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Mar 19, 2024
1 parent 6dc87f3 commit 14b7f0e
Show file tree
Hide file tree
Showing 14 changed files with 1,038 additions and 253 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
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;

@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;
this.headers = builder.headers != null ? ImmutableMap.copyOf( builder.headers ) : ImmutableMap.of();
}

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,6 +2,7 @@

import javax.mail.internet.MimeMessage;

@Deprecated
public interface MailMessage
{
void compose( MimeMessage message )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.enonic.xp.mail;

import com.enonic.xp.annotation.PublicApi;

@PublicApi
public interface MailService
{
@Deprecated
void send( MailMessage message );

void send( SendMailParams message );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package com.enonic.xp.mail;

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

import com.enonic.xp.annotation.PublicApi;

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

private final List<String> from;

private final List<String> cc;

private final List<String> bcc;

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 SendMailParams( Builder builder )
{
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;
}

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

public List<String> getTo()
{
return to;
}

public List<String> getFrom()
{
return from;
}

public List<String> getCc()
{
return cc;
}

public List<String> getBcc()
{
return bcc;
}

public List<String> getReplyTo()
{
return replyTo;
}

public String getSubject()
{
return subject;
}

public String getContentType()
{
return contentType;
}

public String getBody()
{
return body;
}

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

public List<MailAttachment> getAttachments()
{
return attachments;
}

public static class Builder
{
private final List<String> to = new ArrayList<>();

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

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

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

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;

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

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

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

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

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

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

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

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

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

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

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

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

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

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.attachments.add( attachment );
return this;
}

public Builder addAttachments( final Collection<MailAttachment> attachments )
{
this.attachments.addAll( attachments );
return this;
}

public SendMailParams build()
{
return new SendMailParams( this );
}
}
}
1 change: 1 addition & 0 deletions modules/core/core-mail/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dependencies {
implementation project( ':core:core-api' )
implementation project( ':core:core-internal' )

testImplementation ( libs.mockjavamail ) {
exclude group: 'javax.mail'
Expand Down
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();
}
Loading

0 comments on commit 14b7f0e

Please sign in to comment.