All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.enonic.xp.mail.SendMailParams Maven / Gradle / Ivy

The newest version!
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 to;

    private final List from;

    private final List cc;

    private final List bcc;

    private final List replyTo;

    private final List headers;

    private final List 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 getTo()
    {
        return to;
    }

    public List getFrom()
    {
        return from;
    }

    public List getCc()
    {
        return cc;
    }

    public List getBcc()
    {
        return bcc;
    }

    public List getReplyTo()
    {
        return replyTo;
    }

    public String getSubject()
    {
        return subject;
    }

    public String getContentType()
    {
        return contentType;
    }

    public String getBody()
    {
        return body;
    }

    public List getHeaders()
    {
        return headers;
    }

    public List getAttachments()
    {
        return attachments;
    }

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

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

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

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

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

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

        private final List 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 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 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 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 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 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 headers )
        {
            headers.forEach( this::addHeader );
        }

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

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

        public SendMailParams build()
        {
            return new SendMailParams( this );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy