Exemplo n.º 1
0
 public SendResult(ESendResult result, SmtpResponse lastResponse)
 {
     this._invalidAddresses = new List <EmailAddress>();
     if (lastResponse == null)
     {
         throw new ArgumentNullException("lastResponse");
     }
     this.Result       = result;
     this.LastResponse = lastResponse;
 }
Exemplo n.º 2
0
 public SendResult(ESendResult result, SmtpResponse lastResponse, IEnumerable <EmailAddress> invalidEmails)
 {
     this._invalidAddresses = new List <EmailAddress>();
     if (lastResponse == null)
     {
         throw new ArgumentNullException("lastResponse");
     }
     if (invalidEmails == null)
     {
         throw new ArgumentNullException("invalidEmails");
     }
     this.Result = result;
     this._invalidAddresses.AddRange(invalidEmails);
     this.LastResponse = lastResponse;
 }
Exemplo n.º 3
0
        protected virtual IList <SendResult> PerformSendMailInteraction(IEnumerable <SmtpMessage> messages, IConnection connection)
        {
            if (messages == null)
            {
                throw new ArgumentNullException("messages");
            }
            if (connection.State != EConnectionState.Connected)
            {
                throw new InvalidOperationException("Connection should be opened");
            }
            IList <SendResult> list = new List <SendResult>();

            if (this.Connected != null)
            {
                this.Connected(this);
            }
            SmtpResponse lastResponse = new SmtpResponse(connection);

            if (lastResponse.Type != ESmtpResponseType.PositiveCompletion)
            {
                list.Add(new SendResult(ESendResult.StrangeServerResponse, lastResponse));
                return(list);
            }
            if (this.DoAction(new EHLOCommand(), connection).Type != ESmtpResponseType.PositiveCompletion)
            {
                SmtpResponse response3 = this.DoAction(new HELOCommand(), connection);
                if (response3.Type != ESmtpResponseType.PositiveCompletion)
                {
                    list.Add(new SendResult(ESendResult.StrangeServerResponse, response3));
                    return(list);
                }
            }
            if (this._configurationProvider.SSLInteractionType == EInteractionType.StartTLS)
            {
                SmtpResponse response4 = this.DoAction(new StartTlsCommand(), connection);
                if (response4.Type != ESmtpResponseType.PositiveCompletion)
                {
                    list.Add(new SendResult(ESendResult.SslWasNotStarted, response4));
                    return(list);
                }
                connection.SwitchToSslChannel();
            }
            SmtpResponse response2 = this.DoAction(new EHLOCommand(), connection);

            if (response2.Type != ESmtpResponseType.PositiveCompletion)
            {
                SmtpResponse response5 = this.DoAction(new HELOCommand(), connection);
                if (response5.Type != ESmtpResponseType.PositiveCompletion)
                {
                    list.Add(new SendResult(ESendResult.StrangeServerResponse, response5));
                    return(list);
                }
            }
            if (this._configurationProvider.AuthenticationType != EAuthenticationType.None)
            {
                ISmtpAction action = GetAuthenticationCommand(this.Username, this.Password, this._configurationProvider.AuthenticationType, response2.Lines);
                if (action == null)
                {
                    throw new InvalidOperationException("Server doesn't support existed authentication mechanisms or doesn't require any authentication");
                }
                SmtpResponse response6 = this.DoAction(action, connection) as AuthCommandResponse;
                if (response6.Type != ESmtpResponseType.PositiveCompletion)
                {
                    SendResult item = new SendResult(ESendResult.AuthenticationFailed, response6);
                    try
                    {
                        this.DoAction(new QUITCommand(), connection);
                    }
                    catch { }
                    list.Add(item);
                    return(list);
                }
            }
            if (this.Authenticated != null)
            {
                this.Authenticated(this);
            }
            List <EmailAddress> invalidEmails = new List <EmailAddress>();

            foreach (SmtpMessage message in messages)
            {
                SmtpResponse response7 = this.DoAction(new MAILCommand(message.From), connection);
                if (response7.Type != ESmtpResponseType.PositiveCompletion)
                {
                    list.Add(new SendResult(ESendResult.FromAddressFailed, response7));
                }
                else
                {
                    foreach (EmailAddress address in message.To)
                    {
                        if (this.DoAction(new RCPTCommand(address), connection).Type != ESmtpResponseType.PositiveCompletion)
                        {
                            invalidEmails.Add(address);
                        }
                    }
                    foreach (EmailAddress address2 in message.CarbonCopies)
                    {
                        if (this.DoAction(new RCPTCommand(address2), connection).Type != ESmtpResponseType.PositiveCompletion)
                        {
                            invalidEmails.Add(address2);
                        }
                    }
                    foreach (EmailAddress address3 in message.BlindedCarbonCopies)
                    {
                        if (this.DoAction(new RCPTCommand(address3), connection).Type != ESmtpResponseType.PositiveCompletion)
                        {
                            invalidEmails.Add(address3);
                        }
                    }
                    IEnumerable <string> encoded    = MailMessageRFCEncoder.GetEncoded(message);
                    SmtpResponse         response11 = this.DoAction(new DATACommand(encoded), connection);
                    if (response11.Type != ESmtpResponseType.PositiveCompletion)
                    {
                        list.Add(new SendResult(ESendResult.DataSendingFailed, response11, invalidEmails));
                        continue;
                    }
                    if (this.MessageSent != null)
                    {
                        this.MessageSent(this);
                    }
                    if (invalidEmails.Count == 0)
                    {
                        list.Add(new SendResult(ESendResult.Ok, response11));
                        continue;
                    }
                    list.Add(new SendResult(ESendResult.OkWithInvalidEmails, response11, invalidEmails));
                }
            }
            try
            {
                this.DoAction(new QUITCommand(), connection);
            }
            catch { }
            if (this.Completed != null)
            {
                this.Completed(this);
            }
            return(list);
        }