Esempio n. 1
0
        /// <summary>
        /// Send the email.
        /// </summary>
        /// <param name="emailMessage">The completed message</param>
        /// <param name="rcpttocollection">A list of email addresses which
        /// are to be used in the RCPT TO SMTP communication</param>
        /// <param name="mailfrom">An email address for the MAIL FROM
        /// part of the SMTP protocol.</param>
        /// <exception cref="SmtpException">throws an SmtpException if there
        /// is an unexpected SMTP error number sent from the server.</exception>
        /// <exception cref="MailException">throws a MailException if there
        /// is a network problem, connection problem, or other issue.</exception>
        /// <returns></returns>
        internal bool Send(ISendableMessage emailMessage, EmailAddressCollection rcpttocollection, EmailAddress mailfrom)
        {
            //ISmtpNegotiator negotiator=_smtpserver.GetSmtpNegotiator();
            ISmtpProxy smtpproxy = GetSmtpProxy();
            //smtpproxy.CaptureSmtpConversation=CaptureSmtpConversation;
            SmtpResponse smtpResponse = smtpproxy.Open();

            try
            {
                #region Connect
                if (smtpResponse.ResponseCode != 220)
                {
                    throw smtpResponse.GetException();
                }
                #endregion

                #region HELO / EHLO
                if (UseEhlo())
                {
                    EhloSmtpResponse esmtpResponse = smtpproxy.Ehlo(GetHeloHost());
                    if (esmtpResponse.ResponseCode != 250)
                    {
                        // TODO: FIX THIS
                        throw new SmtpException(esmtpResponse.ResponseCode, esmtpResponse.Message);
                    }

                    // do SMTP AUTH
                    if (this._authToken != null)
                    {
                        smtpResponse = _authToken.Negotiate(smtpproxy, esmtpResponse.GetAvailableAuthTypes());
                        if (smtpResponse.ResponseCode != 235)
                        {
                            throw smtpResponse.GetException();
                        }
                    }
                }
                else
                {
                    smtpResponse = smtpproxy.Helo(GetHeloHost());
                    if (smtpResponse.ResponseCode != 250)
                    {
                        throw smtpResponse.GetException();
                    }
                }
                #endregion

                #region MAIL FROM
                smtpResponse = smtpproxy.MailFrom(mailfrom);
                if (smtpResponse.ResponseCode != 250)
                {
                    throw smtpResponse.GetException();
                }
                #endregion

                #region RCPT TO

                foreach (EmailAddress rcpttoaddress in rcpttocollection)
                {
                    smtpResponse = smtpproxy.RcptTo(rcpttoaddress);
                    if (smtpResponse.ResponseCode != 250)
                    {
                        throw smtpResponse.GetException();
                    }
                }
                #endregion

                #region DATA

                smtpResponse = smtpproxy.Data();
                if (smtpResponse.ResponseCode != 354)
                {
                    throw smtpResponse.GetException();
                }
                //smtpResponse=negotiator.WriteData();

                String message = emailMessage.ToDataString();
                if (message == null)
                {
                    throw new MailException("The message content is null");
                }

                /*
                 * // START Test With Domain Keys
                 * // (this would appear as an event callback if it works)
                 * MailSigner mailsigner=new MailSigner();
                 * bool signed = mailsigner.signMail(message);
                 * if (!signed)
                 * {
                 *      throw new MailException("Error creating DomainKeys signature.");
                 * }
                 * message=mailsigner.signedHeader + message;
                 * // END Test With Domain Keys
                 */


                // Send the data
                smtpResponse = smtpproxy.WriteData(message);
                if (smtpResponse.ResponseCode != 250)
                {
                    throw smtpResponse.GetException();
                }
                #endregion

                #region QUIT
                // QUIT
                smtpResponse = smtpproxy.Quit();
                if (smtpResponse.ResponseCode != 221)
                {
                    throw smtpResponse.GetException();
                }
                #endregion
            }
            finally
            {
                smtpproxy.Close();
                OnLogSmtpCompleted(this, "Connection Closed");
            }
            return(true);
        }