private void sendMessageButton_Click(object sender, EventArgs e)
        {
            this.AddLogEntry("Creating message.");

            // We create the message object
            ActiveUp.Net.Mail.Message message = new ActiveUp.Net.Mail.Message();

            // We assign the sender email
            message.From.Email = this.fromEmailTextbox.Text;

            // We assign the recipient email
            message.To.Add(this.toEmailTextbox.Text);

            // We assign the subject
            message.Subject = this.subjectTextbox.Text;

            // We assign the body text
            message.BodyText.Text = this.bodyTextTextbox.Text;

            // We send the email using the specified SMTP server
            this.AddLogEntry("Sending message.");

            try
            {
                SmtpClient smtpClient = new SmtpClient();

                SslHandShake handShake = new SslHandShake("mail.activeup.com", System.Security.Authentication.SslProtocols.Ssl3);
                handShake.ServerCertificateValidationCallback = MyServerCertificateValidationCallback;

                this.AddLogEntry("Message sent successfully.");
            }
            catch (SmtpException ex)
            {
                this.AddLogEntry(string.Format("Smtp Error: {0}", ex.Message));
            }
            catch (Exception ex)
            {
                this.AddLogEntry(string.Format("Failed: {0}", ex.Message));
            }
        }
        protected virtual void DoSslHandShake(SslHandShake sslHandShake)
        {
            Logger.AddEntry("DoSslHandShake:Creating SslStream...", 2);
            this._sslStream = new SslStream(base.GetStream(), false,
                                            CertificatePermit
                                                                    ? (sender, certificate, chain, sslPolicyErrors) => true
                                                                    : sslHandShake.ServerCertificateValidationCallback,
                                            sslHandShake.ClientCertificateSelectionCallback);
            Logger.AddEntry("DoSslHandShake:AuthenticateAsClient...", 2);

            try
            {
                this._sslStream.AuthenticateAsClient(sslHandShake.HostName, sslHandShake.ClientCertificates,
                                                     sslHandShake.SslProtocol, sslHandShake.CheckRevocation);
            }
            catch (Exception ex)
            {
                Logger.AddEntry(string.Format("DoSslHandShake:AuthenticateAsClient failed with Exception {0}", ex), 2);
                this._sslStream = null;
                throw;
            }
        }
Exemple #3
0
    public Boolean SendSessionedMail(Boolean UseSSL, Boolean UseMD5, System.Security.Authentication.SslProtocols protocol)
    {
        Message message = new Message();

        ServerCollection Hosts = null;
        int    Port            = 0;
        string UserName        = string.Empty;
        string Password        = string.Empty;
        bool   PORTExist       = false;

        try
        {
            if (Session["From"] != null)
            {
                message.From.Email = Session["From"].ToString();
            }
            else
            {
                return(false);
                //throw new Exception("From Email is a mandatory information");
            }
            if (Session["FromName"] != null)
            {
                message.From.Name = Session["FromName"].ToString();
            }
            else
            {
                return(false);
                //throw new Exception("From Name is a mandatory information");
            }

            if (Session["To"] != null)
            {
                message.To.AddRange((AddressCollection)Session["To"]);
            }
            else
            {
                return(false);
                //throw new Exception("Email to is a mandatory information");
            }
            if (Session["CC"] != null)
            {
                message.Cc.AddRange((AddressCollection)Session["CC"]);
            }


            if (Session["BCC"] != null)
            {
                message.Bcc.AddRange((AddressCollection)Session["BCC"]);
            }

            if (Session["Subject"] != null)
            {
                message.Subject = Session["Subject"].ToString();
            }
            else
            {
                //May not be needed
                //throw new Exception("Subject to is a mandatory information");
            }
            if (Session["BodyText"] != null)
            {
                message.BodyText.Text = Session["BodyText"].ToString();
            }

            if (Session["BodyHTML"] != null)
            {
                message.BodyHtml.Text = Session["BodyHTML"].ToString();
            }


            if (Session["Hosts"] != null)
            {
                Hosts = (ServerCollection)Session["Hosts"];
            }

            if (Session["Port"] != null)
            {
                Port      = (int)Session["Port"];
                PORTExist = true;
            }

            if (Session["User"] != null)
            {
                UserName = (string)Session["User"];
            }

            if (Session["Password"] != null)
            {
                Password = (string)Session["Password"];
            }

            //TODO - Need to find a way to manage multiple attachemnts
            // Session is not a solution as it will generate too much network traffic
            // Probably need to store it locally and associate with Session ID

            if (UserName.Length > 0 && Password.Length > 0 && Hosts.Count > 0 && UseSSL)
            {
                //Use SSL to send mail
                SmtpClient   smtpClient = new SmtpClient();
                SslHandShake handShake  = new SslHandShake(Hosts[0].Host, protocol);
                handShake.ServerCertificateValidationCallback = MyServerCertificateValidationCallback;
                message.Send(Hosts[0].Host, UserName, Password, UseMD5 ? SaslMechanism.CramMd5 : SaslMechanism.Login);
                return(true);
            }
            else if (UserName.Length > 0 && Password.Length > 0 && Hosts.Count > 0 && PORTExist)
            {
                //Use Authentication for sending mail
                message.Send(Hosts[0].Host, Port, UserName, Password, UseMD5 ? SaslMechanism.CramMd5 : SaslMechanism.Login);
                return(true);
            }
            else if ((UserName.Trim().Length == 0 || Password.Length == 0) && Hosts.Count > 0 && PORTExist)
            {
                //Use specified port for sending mail
                message.Send(Hosts[0].Host, Port);
                return(true);
            }
            else if ((UserName.Trim().Length == 0 || Password.Length == 0) && Hosts.Count > 0 && !PORTExist)
            {
                //Send without authentication
                message.Send(Hosts);
                return(true);
            }
            else
            {
                //Send directly without specifying a server
                message.DirectSend();
                return(true);
            }
            return(false);
        }
        catch (Exception ex)
        {
            return(false);
        }
        finally
        {
            message = null;
        }
    }