Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            SMTPMessage smtpMessage = new SMTPMessage();

            smtpMessage.Password      = MailInformation.password;
            smtpMessage.MailAddress   = MailInformation.mailAddress;
            smtpMessage.Username      = MailInformation.userName;
            smtpMessage.TargetAddress = mailToBox.Text;
            smtpMessage.Subject       = subjectBox.Text;
            smtpMessage.Message       = messageBox.Text;


            bool value = mailClient.SendMessage(smtpMessage);

            string message;

            if (value)
            {
                message = "Wiadomość została pomyślnie wysłana";
            }
            else
            {
                message = "Nie udało się wysłać wiadomości";
            }

            MessageBoxButtons buttons = MessageBoxButtons.OK;
            DialogResult      result;

            // Displays the MessageBox.

            result = MessageBox.Show(message, "INFORMACJA", buttons);
        }
        public bool SendMessage(SMTPMessage smtpMessage)
        {
            CodeBase64 codeBase64 = new CodeBase64();

            string b64Login    = codeBase64.EncodeBase64(smtpMessage.Username);
            string b64Password = codeBase64.EncodeBase64(smtpMessage.Password);


            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(_serverAddress, 587);
            socket.ReceiveTimeout = 1000;

            string outputString;


            socket.Send(Encoding.UTF8.GetBytes("helo\r\n"));
            socket.Send(Encoding.UTF8.GetBytes("auth login\r\n"));
            outputString = GetCurrentSocketInfo(socket);
            socket.Send(Encoding.UTF8.GetBytes(b64Login + "\r\n"));
            outputString = GetCurrentSocketInfo(socket);
            socket.Send(Encoding.UTF8.GetBytes(b64Password + "\r\n"));
            outputString = GetCurrentSocketInfo(socket);

            socket.Send(Encoding.UTF8.GetBytes("mail from:<" + smtpMessage.Username + "@o2.pl>\n"));
            outputString = GetCurrentSocketInfo(socket);



            if (outputString.Contains("250") || outputString.Contains("235"))
            {
                socket.Send(Encoding.UTF8.GetBytes("rcpt to:<" + smtpMessage.TargetAddress + ">\n"));
                outputString = GetCurrentSocketInfo(socket);
                socket.Send(Encoding.UTF8.GetBytes("DATA\r\n"));
                outputString = GetCurrentSocketInfo(socket);
                socket.Send(Encoding.UTF8.GetBytes("From:<" + smtpMessage.Username + ">\n" + "To:<" + smtpMessage.TargetAddress + ">\nSubject:" + smtpMessage.Subject + "\nContent:<" + smtpMessage.Message + "\r\n.\n"));


                outputString = GetCurrentSocketInfo(socket);

                if (outputString.Contains("250"))
                {
                    return(true);
                }
            }
            return(false);
        }