Exemplo n.º 1
0
        /// <summary>
        /// Parse an Smtp response text into SmtpReply
        /// </summary>
        /// <param name="textReply">Smtp response text</param>
        /// <returns>SmtpReply parsed from text</returns>
        public static SmtpReply Parse(string textReply)
        {
            SmtpReply smtpReply = new SmtpReply();

            // extract reply code (first 3 digits)
            smtpReply.ReplyCode = (SmtpReplyCode)Convert.ToInt32(textReply.Substring(0, SmtpReply.REPLY_CODE_LENGTH));
            // extract the text after reply code
            smtpReply.Text = textReply.Substring(3, textReply.IndexOf(SmtpReply.CRLF) - 3).Trim();

            return(smtpReply);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Send an email message
        /// </summary>
        /// <param name="emailMessage">Email message to send</param>
        public void Send(EmailMessage emailMessage)
        {
            try
            {
                // get Smtp server Ip address
                IPHostEntry smtpServerHostEntry = Dns.GetHostEntry(this.smtpServer.Host);
                // create socket to connect to the Smtp server
                this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                this.socket.Connect(new IPEndPoint(smtpServerHostEntry.AddressList[0], this.smtpServer.Port));
            }
            catch (Exception ex)
            {
                throw new SmtpCommunicationException(ex);
            }

            SmtpReply   smtpResponse;
            SmtpCommand smtpCommand;

            smtpResponse = SmtpReply.Parse(this.GetResponse());
            if (smtpResponse.ReplyCode != SmtpReplyCode.DomainServiceReady)
            {
                throw new SmtpException(smtpResponse.ReplyCode);
            }

            // HELO command
            smtpCommand             = new SmtpCommand();
            smtpCommand.CommandCode = SmtpCommandCode.HELO;
            smtpCommand.Text        = this.clientHostName;
            this.SendCommand(smtpCommand);
            smtpResponse = SmtpReply.Parse(this.GetResponse());
            if (smtpResponse.ReplyCode != SmtpReplyCode.RequestedMailActionOk)
            {
                throw new SmtpException(smtpResponse.ReplyCode);
            }

            // FROM command
            smtpCommand.CommandCode = SmtpCommandCode.MAIL;
            smtpCommand.Text        = "FROM:<" + emailMessage.From + ">";
            this.SendCommand(smtpCommand);
            smtpResponse = SmtpReply.Parse(this.GetResponse());
            if (smtpResponse.ReplyCode != SmtpReplyCode.RequestedMailActionOk)
            {
                throw new SmtpException(smtpResponse.ReplyCode);
            }

            // TO command, for all recipients
            foreach (string rcpt in emailMessage.To)
            {
                smtpCommand.CommandCode = SmtpCommandCode.RCPT;
                smtpCommand.Text        = "TO:<" + rcpt + ">";
                this.SendCommand(smtpCommand);
                smtpResponse = SmtpReply.Parse(this.GetResponse());
                if (smtpResponse.ReplyCode != SmtpReplyCode.RequestedMailActionOk)
                {
                    throw new SmtpException(smtpResponse.ReplyCode);
                }
            }

            // DATA command
            smtpCommand.CommandCode = SmtpCommandCode.DATA;
            smtpCommand.Text        = String.Empty;
            this.SendCommand(smtpCommand);
            smtpResponse = SmtpReply.Parse(this.GetResponse());
            if (smtpResponse.ReplyCode != SmtpReplyCode.StartMailInput)
            {
                throw new SmtpException(smtpResponse.ReplyCode);
            }

            // mail text
            this.SendRawData(emailMessage.ToString());
            smtpResponse = SmtpReply.Parse(this.GetResponse());
            if (smtpResponse.ReplyCode != SmtpReplyCode.RequestedMailActionOk)
            {
                throw new SmtpException(smtpResponse.ReplyCode);
            }

            // QUIT command
            smtpCommand.CommandCode = SmtpCommandCode.QUIT;
            smtpCommand.Text        = String.Empty;
            this.SendCommand(smtpCommand);
            smtpResponse = SmtpReply.Parse(this.GetResponse());
            if (smtpResponse.ReplyCode != SmtpReplyCode.DomainServiceClosingTransmissionChannel)
            {
                throw new SmtpException(smtpResponse.ReplyCode);
            }

            this.socket.Close();
        }