Пример #1
0
        //---------------------------------------------------------------------------------------------------------------------------------------------------------------
        public static bool Test()
        {
            MGLSecureEmailer.BodyIsHTML           = true;
            MGLSecureEmailer.DigitalSignatureFile = new SecureString();
            foreach (char c in "C:/Docs/OpenSSL_DataNirvana/UNHCRPakIMTeam/UNHCRPakIMTeamCertificate.pfx".ToCharArray())
            {
                MGLSecureEmailer.DigitalSignatureFile.AppendChar(c);
            }

            string       username = "******";
            SecureString userSS   = new SecureString();

            foreach (char c in username.ToCharArray())
            {
                userSS.AppendChar(c);
            }

            string       password = "";
            SecureString pwordSS  = new SecureString();

            foreach (char c in password.ToCharArray())
            {
                pwordSS.AppendChar(c);
            }

            SecureString userNameSS = new SecureString();

            foreach (char c in "Pak IM Team".ToCharArray())
            {
                userNameSS.AppendChar(c);
            }

            bool success = MGLSecureEmailer.SendEmail(
                new StringBuilder("*****@*****.**"),
                new StringBuilder("Pak IM Team"),
                "Testing the MGLSecureEmailer",
                "And Hi<br />This is indeed a test<br />Forget the rest<br />MGLSecureEmailer is <b>best</b>!",
                "smtp.gmail.com", userSS, pwordSS,
                userSS, userNameSS, 587, true);

            return(success);
        }
Пример #2
0
        //----------------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///    Sends an email signed with a digital signature specified by the X509 certificate file name supplied in the DigitalSignatureFile
        ///    property
        /// </summary>
        /// <param name="toMailAddress"></param>
        /// <param name="toName">Leave blank "" for default</param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="SMTPHost">Leave blank "" for default</param>
        /// <param name="SMTPUsername">Leave blank "" for default</param>
        /// <param name="SMTPPassword">Leave blank "" for default</param>
        /// <param name="fromMailAddress">Leave blank "" for default</param>
        /// <param name="fromName">Leave blank "" for default</param>
        /// <param name="portID">Leave 0 default</param>
        /// <param name="EnableSSL"></param>
        /// <returns>True if the email was sent successfully</returns>
        public static bool SendEmail(
            StringBuilder toMailAddress, StringBuilder toName,
            string subject, string body,
            string SMTPHost, SecureString SMTPUsername, SecureString SMTPPassword,
            SecureString fromMailAddress, SecureString fromName,
            int portID, bool EnableSSL)
        {
            bool isSuccess = true;
            // instantiate here so we can use the finally block to clean up this message ...
            SecureMailMessage message = null;
            X509Certificate2  myCert  = null;

            try {
                //_____0_____ Check the SMTP details ...
                CheckSMTPDetails(ref fromMailAddress, ref fromName, ref SMTPHost, ref SMTPUsername, ref SMTPPassword, ref portID);

                //_____1_____ Lets start the message - note we just want to add the from, to and subject for now
                // If this email is to be signed then we need to keep the body blank ...
                message = new SecureMailMessage();

                // Load the recipient's encryption cert from a file.
                myCert = new X509Certificate2(MGLSecureEmailer.Decrypt(digitalSignatureFile).ToString());

                // Get the digest for this certificate ...
                SetDigest(myCert);

                // Set the from and to user - note that we can add multiple users with this email format
                message.From = new SecureMailAddress(
                    MGLSecureEmailer.Decrypt(fromMailAddress).ToString(),
                    MGLSecureEmailer.Decrypt(fromName).ToString(), null, myCert);

                message.To.Add(new SecureMailAddress(toMailAddress.ToString(), toName.ToString(), null));
                //message.To.Add(new SecureMailAddress("*****@*****.**", "Pak IM Team", null));

                // Add the subject
                message.Subject = subject;

                // Add the body
                message.Body       = body;
                message.IsBodyHtml = bodyIsHTML;

                // We currently ONLY want to SIGN the email, not encrypt it, so set these two values appropriately
                // (and we ALWAYS want to sign it) ...
                message.IsSigned    = true;
                message.IsEncrypted = false;


                //_____2_____ Setup the SMTP client
                System.Net.Mail.SmtpClient smtpClient = new SmtpClient(SMTPHost);

                // The checkSMTPDetails method above has already tried to check if the portID is zero or less
                // So will already be the default value if this is the case, so we can roll right on
                smtpClient = new SmtpClient(SMTPHost, portID);

                // Sort the credentials to be used for the SMTP connection out
                System.Net.NetworkCredential ncAuth = new System.Net.NetworkCredential(
                    MGLSecureEmailer.Decrypt(SMTPUsername).ToString(),
                    MGLSecureEmailer.Decrypt(SMTPPassword).ToString()
                    );

                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials           = ncAuth;

                // To use ssl or not that is the question
                // With the SMTP connection we probably always now want to use SSL or even better, TLS
                if (EnableSSL || SMTPHost.Contains("gmail"))
                {
                    smtpClient.EnableSsl = true;
                }


                //_____3_____ Lets do it!  Send the email - this is neat as the SecureMailMessage can be cast as a MailMessage
                // which is the input that the smtpClient is expecting ...
                smtpClient.Send(message);
            } catch (Exception ex) {
                // Add the exception to the last exception property and ensure that the user knows this Email was not sent by returning false
                isSuccess = false;
                MGLSecureEmailer.LastException = ex;
            } finally {
                //_____4_____ And lastly, lets clean up everything that contains sensitive passwords or private keys
                if (message != null)
                {
                    message.Dispose();
                }

                // and lets also dispose of the certificate information that is being stored in memory ...
                if (myCert != null)
                {
                    myCert = null;
                    DigitalSignatureDigestOID = null;
                }
            }

            return(isSuccess);
        }