예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EmailParticipants"/> class.
        /// </summary>
        /// <param name="from">The mailbox of the sender of the email.</param>
        /// <param name="to">The email mailboxes of the recipients of the email.</param>
        /// <param name="cc">OPTIONAL email mailboxes of the carbon copy recipients of the email.  DEFAULT is null; the email will have no carbon copy recipients.</param>
        /// <param name="bcc">OPTIONAL email mailboxes of the blind carbon copy recipients of the email.  DEFAULT is null; the email will have no blind carbon copy recipients.</param>
        /// <param name="replyTo">OPTIONAL email mailboxes that the recipients (regular, cc, bcc) of the email should reply to.  DEFAULT is null; recipients should reply to <paramref name="to"/>.</param>
        public EmailParticipants(
            EmailMailbox from,
            IReadOnlyCollection <EmailMailbox> to,
            IReadOnlyCollection <EmailMailbox> cc      = null,
            IReadOnlyCollection <EmailMailbox> bcc     = null,
            IReadOnlyCollection <EmailMailbox> replyTo = null)
        {
            new { from }.AsArg().Must().NotBeNull();
            new { to }.AsArg().Must().NotBeNullNorEmptyEnumerableNorContainAnyNulls();

            if (cc != null)
            {
                new { cc }.AsArg().Must().NotContainAnyNullElements();
            }

            if (bcc != null)
            {
                new { bcc }.AsArg().Must().NotContainAnyNullElements();
            }

            if (replyTo != null)
            {
                new { replyTo }.AsArg().Must().NotContainAnyNullElements();
            }

            this.From    = from;
            this.To      = to;
            this.Cc      = cc;
            this.Bcc     = bcc;
            this.ReplyTo = replyTo;
        }
        public EmailParticipants DeepCloneWithFrom(EmailMailbox from)
        {
            var result = new EmailParticipants(
                from,
                this.To?.DeepClone(),
                this.Cc?.DeepClone(),
                this.Bcc?.DeepClone(),
                this.ReplyTo?.DeepClone());

            return(result);
        }
        /// <summary>
        /// Converts a <see cref="EmailMailbox"/> to an <see cref="MailAddress"/>.
        /// </summary>
        /// <param name="emailMailbox">The email mailbox to convert.</param>
        /// <returns>
        /// The <see cref="MailAddress"/> that corresponds to the specified <see cref="EmailMailbox"/>.
        /// </returns>
        public static MailAddress ToMailAddress(
            this EmailMailbox emailMailbox)
        {
            new { emailMailbox }.AsArg().Must().NotBeNull();

            new { emailMailbox.Address }.AsArg(Invariant($"{nameof(emailMailbox)}.{nameof(EmailMailbox.Address)}")).Must().BeValidEmailAddress();

            var displayName = string.IsNullOrWhiteSpace(emailMailbox.Name) ? null : emailMailbox.Name;

            var result = new MailAddress(emailMailbox.Address, displayName);

            return(result);
        }