Example #1
0
        public override Task ExecuteAsync(CancellationToken token)
        {
            if (_builder.PendingMail != null)
            {
                return(_channel.SendReplyAsync(SmtpReplyCode.BadSequence, "MAIL not allowed now", CancellationToken.None));
            }

            Match fromMatch = s_fromExpression.Match(Arguments);

            if (!fromMatch.Success)
            {
                return(_channel.SendReplyAsync(
                           SmtpReplyCode.InvalidArguments,
                           "Bad FROM address",
                           CancellationToken.None));
            }

            string sourceRoute   = fromMatch.Groups[1].Value;
            string mailbox       = fromMatch.Groups[2].Value;
            string parameterText = fromMatch.Groups[3].Value;

            if (!string.IsNullOrEmpty(sourceRoute))
            {
                return(_channel.SendReplyAsync(
                           SmtpReplyCode.InvalidArguments,
                           "Return path not supported",
                           CancellationToken.None));
            }

            if (!TryProcessParameterValue(_channel, parameterText, out Task errorReport, token))
            {
                return(errorReport);
            }

            if (_channel.IsAuthenticated &&
                !_userStore.CanUserSendAs(_channel.AuthenticatedUser, mailbox))
            {
                return(_channel.SendReplyAsync(SmtpReplyCode.MailboxUnavailable, "Invalid Mailbox", token));
            }

            if (!_channel.IsAuthenticated &&
                _settings.LocalDomains?.Any(
                    d => string.Equals(
                        d.Name,
                        MailUtilities.GetDomainFromMailbox(mailbox),
                        StringComparison.OrdinalIgnoreCase)
                    ) == true
                )
            {
                return(_channel.SendReplyAsync(
                           SmtpReplyCode.InvalidArguments,
                           "Must be signed in to send from domain",
                           token));
            }

            _builder.PendingMail = new SmtpMailMessage(
                new SmtpPath(
                    mailbox));

            return(_channel.SendReplyAsync(SmtpReplyCode.Okay, token));
        }