Exemple #1
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();
        }
Exemple #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();
        }
Exemple #3
0
 /// <summary>
 /// Send a command to the Smtp server
 /// </summary>
 /// <param name="smtpCommand">Command to send</param>
 private void SendCommand(SmtpCommand smtpCommand)
 {
     this.SendRawData(smtpCommand.ToString());
 }
Exemple #4
0
 /// <summary>
 /// Send a command to the Smtp server
 /// </summary>
 /// <param name="smtpCommand">Command to send</param>
 private void SendCommand(SmtpCommand smtpCommand)
 {
     this.SendRawData(smtpCommand.ToString());
 }