Пример #1
0
        /// <summary>
        /// Simple E-Mail Function, you just need Subject and Message
        /// the rest is taken from the GlobalVars
        /// </summary>
        /// <param name="subject">Subject for the Mail</param>
        /// <param name="message">Body Message for the Mail</param>
        public static void Send(string subject, string message, bool showDebug)
        {
            bool mailError = false;
            try
            {
                MailMessage msg = new MailMessage();
                msg.EmailFrom = GlobalVars.config_mail_from;
                msg.AddEmailTo(GlobalVars.config_mail_to);
                msg.EmailMessageType = MessageType.Text;
                msg.EmailMessage = message;
                msg.EmailSubject = subject;
                Smtp smtp = new Smtp();

                smtp.SmtpPort = System.Convert.ToInt16(GlobalVars.config_mail_server_port);
                smtp.SmtpServer = GlobalVars.config_mail_server;
                smtp.SmtpUser = GlobalVars.config_mail_user;
                smtp.SmtpPassword = GlobalVars.config_mail_password;
                smtp.SendEmail(msg);
            }
            catch (SmtpException e)
            {
                if (showDebug)
                {
                    mailError = true;
                    MessageBox.Show(e.ToString());
                }
            }
            if (showDebug && !mailError)
            {
                MessageBox.Show(GlobalVars.lang_mailsuccessfullysent);
            }
        }
Пример #2
0
        /// <summary>
        /// Sends the e-mail based on the properties set for this object
        /// </summary>
        /// <param name="msg">MailMessage</param>
        public void SendEmail(MailMessage msg)
        {
            if (_serverSmtp == "" || msg.EmailFrom == "" || msg.EmailSubject == "" || msg.EmailTo == null)
            {
                throw new SmtpException("Invalid Smtp or email parameters.");
            }

            // open a connection to the Smtp server
            using (TcpClient smtpSocket = new TcpClient(_serverSmtp, _portSmtp))
            using (NetworkStream ns = smtpSocket.GetStream())
            {
                // get response from Smtp server
                int code = GetSmtpResponse(ReadBuffer(ns));
                if (code != 220)
                {
                    throw new SmtpException("Error connecting to Smtp server. (" + code + ")");
                }
                // EHLO
                WriteBuffer(ns, "ehlo UTAC\r\n");
                // get response from Smtp server
                string buffer = ReadBuffer(ns);
                code = GetSmtpResponse(buffer);
                if (code != 250)
                {
                    throw new SmtpException("Error initiating communication with Smtp server. (" + code + ")");
                }
                // check for AUTH=LOGIN
                if (buffer.IndexOf("AUTH=LOGIN") >= 0)
                {
                    // AUTH LOGIN
                    WriteBuffer(ns, "auth login\r\n");
                    // get response from Smtp server
                    code = GetSmtpResponse(ReadBuffer(ns));
                    if (code != 334)
                    {
                        throw new SmtpException("Error initiating Auth=Login. (" + code + ")");
                    }

                    // username:
                    WriteBuffer(ns, Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(_userSmtp)) + "\r\n");
                    // get response from Smtp server
                    code = GetSmtpResponse(ReadBuffer(ns));
                    if (code != 334)
                    {
                        throw new SmtpException("Error setting Auth user name. (" + code + ")");
                    }
                    // password:
                    WriteBuffer(ns, Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(_passwordSmtp)) + "\r\n");
                    // get response from Smtp server
                    code = GetSmtpResponse(ReadBuffer(ns));
                    if (code != 235)
                    {
                        throw new SmtpException("Error setting Auth password. (" + code + ")");
                    }
                }

                // MAIL FROM:
                WriteBuffer(ns, "mail from: <" + msg.EmailFrom + ">\r\n");
                // get response from Smtp server
                code = GetSmtpResponse(ReadBuffer(ns));
                if (code != 250)
                {
                    throw new SmtpException("Error setting sender email address. (" + code + ")");
                }

                // RCPT TO:
                foreach (string sEmailTo in msg.EmailTo)
                {
                    WriteBuffer(ns, "rcpt to:<" + sEmailTo + ">\r\n");
                    // get response from Smtp server
                    code = GetSmtpResponse(ReadBuffer(ns));
                    if (code != 250 && code != 251)
                    {
                        throw new SmtpException("Error setting receipient email address. (" + code + ")");
                    }
                }
                // DATA
                WriteBuffer(ns, "data\r\n");
                // get response from Smtp server
                code = GetSmtpResponse(ReadBuffer(ns));
                if (code != 354)
                {
                    throw new SmtpException("Error starting email body. (" + code + ")");
                }
                // Repeat the from and to addresses in the data section
                WriteBuffer(ns, "from:<" + msg.EmailFrom + ">\r\n");
                foreach (string sEmailTo in msg.EmailTo)
                {
                    WriteBuffer(ns, "to:<" + sEmailTo + ">\r\n");
                }
                WriteBuffer(ns, "date: " + DateTime.Now.ToString("r") + ">\r\n"); // Inserted for compatibility 2010.01.05
                WriteBuffer(ns, "Subject:" + msg.EmailSubject + "\r\n");
                switch (msg.EmailMessageType)
                {
                    case MessageType.Text:
                        // send text message
                        WriteBuffer(ns, "\r\n" + msg.EmailMessage + "\r\n.\r\n");
                        break;
                    case MessageType.HTML:
                        // send HTML message
                        WriteBuffer(ns, "MIME-Version: 1.0\r\n");
                        WriteBuffer(ns, "Content-type: text/html\r\n");
                        WriteBuffer(ns, "\r\n" + msg.EmailMessage + "\r\n.\r\n");
                        break;
                }
                // get response from Smtp server
                code = GetSmtpResponse(ReadBuffer(ns));
                if (code != 250)
                {
                    throw new SmtpException("Error setting email body. (" + code + ")");
                }

                // QUIT
                WriteBuffer(ns, "quit\r\n");

            }
        }