Papercut.Smtp.SmtpClient.Send C# (CSharp) Méthode

Send() public méthode

The send.
///
public Send ( ) : void
Résultat void
        public void Send()
        {
            string response;

            this.Connect(this.session.Sender, 25);
            response = this.Response();
            if (response.Substring(0, 3) != "220")
            {
                throw new SmtpException(response);
            }

            this.Write("HELO {0}\r\n", Util.GetIPAddress());
            response = this.Response();
            if (response.Substring(0, 3) != "250")
            {
                throw new SmtpException(response);
            }

            this.Write("MAIL FROM:<{0}>\r\n", this.session.MailFrom);
            response = this.Response();
            if (response.Substring(0, 3) != "250")
            {
                throw new SmtpException(response);
            }

            this.session.Recipients.ForEach(
                address =>
                    {
                        this.Write("RCPT TO:<{0}>\r\n", address);
                        response = this.Response();
                        if (response.Substring(0, 3) != "250")
                        {
                            throw new SmtpException(response);
                        }
                    });

            this.Write("DATA\r\n");
            response = this.Response();
            if (response.Substring(0, 3) != "354")
            {
                throw new SmtpException(response);
            }

            NetworkStream stream = this.GetStream();
            stream.Write(this.session.Message, 0, this.session.Message.Length);

            this.Write("\r\n.\r\n");
            response = this.Response();
            if (response.Substring(0, 3) != "250")
            {
                throw new SmtpException(response);
            }

            this.Write("QUIT\r\n");
            response = this.Response();
            if (response.IndexOf("221") == -1)
            {
                throw new SmtpException(response);
            }
        }

Usage Example

        void sendButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(server.Text) || string.IsNullOrEmpty(from.Text) || string.IsNullOrEmpty(to.Text))
            {
                MessageBox.Show("All the text boxes are required, fill them in please.", "Papercut", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if(!emailRegex.IsMatch(from.Text) || !emailRegex.IsMatch(to.Text))
            {
                MessageBox.Show("You need to enter valid email addresses.", "Papercut", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            SmtpSession session = new SmtpSession();
            session.Sender = server.Text.Trim();
            session.MailFrom = from.Text;
            session.Recipients.Add(to.Text);
            session.Message = File.ReadAllBytes(messageFilename);

            worker = new BackgroundWorker();

            worker.DoWork += delegate(object s, DoWorkEventArgs args)
            {
                SmtpSession _session = args.Argument as SmtpSession;
                SmtpClient client = new SmtpClient(_session);
                client.Send();
            };

            worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
            {
                // Save settings for the next time
                Settings.Default.ForwardServer = server.Text;
                Settings.Default.ForwardTo = to.Text;
                Settings.Default.ForwardFrom = from.Text;
                Settings.Default.Save();

                working = false;
                sendingLabel.Visibility = Visibility.Hidden;
                DialogResult = true;
            };

            working = true;
            sendButton.IsEnabled = false;
            sendingLabel.Visibility = Visibility.Visible;
            worker.RunWorkerAsync(session);
        }
All Usage Examples Of Papercut.Smtp.SmtpClient::Send