Example #1
0
        protected override void Execute(CodeActivityContext context)
        {
            SmtpClient smtpClient = new SmtpClient();

            if (!string.IsNullOrEmpty(SmtpHost.Get(context)))
            {
                smtpClient.Host = SmtpHost.Get(context);
            }
            if (SmtpPort.Get(context).HasValue)
            {
                smtpClient.Port = SmtpPort.Get(context).Value;
            }
            if (!string.IsNullOrEmpty(Username.Get(context)))
            {
                smtpClient.Credentials = new System.Net.NetworkCredential(Username.Get(context), Password.Get(context));
            }
            smtpClient.EnableSsl = EnableSsl.Get(context);

            MailMessage mailMessage = new MailMessage();

            mailMessage.To.Add(To.Get(context));
            mailMessage.From       = new MailAddress(From.Get(context));
            mailMessage.Subject    = Subject.Get(context);
            mailMessage.Body       = Body.Get(context);
            mailMessage.IsBodyHtml = IsBodyHtml.Get(context);

            if (SendAsync.Get(context))
            {
                smtpClient.SendAsync(mailMessage, null);
            }
            else
            {
                smtpClient.Send(mailMessage);
            }
        }
        /// <summary>
        /// Returns true if ComDayCqMailerDefaultMailServiceProperties instances are equal
        /// </summary>
        /// <param name="other">Instance of ComDayCqMailerDefaultMailServiceProperties to be compared</param>
        /// <returns>Boolean</returns>
        public bool Equals(ComDayCqMailerDefaultMailServiceProperties other)
        {
            if (other is null)
            {
                return(false);
            }
            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            return
                ((
                     SmtpHost == other.SmtpHost ||
                     SmtpHost != null &&
                     SmtpHost.Equals(other.SmtpHost)
                     ) &&
                 (
                     SmtpPort == other.SmtpPort ||
                     SmtpPort != null &&
                     SmtpPort.Equals(other.SmtpPort)
                 ) &&
                 (
                     SmtpUser == other.SmtpUser ||
                     SmtpUser != null &&
                     SmtpUser.Equals(other.SmtpUser)
                 ) &&
                 (
                     SmtpPassword == other.SmtpPassword ||
                     SmtpPassword != null &&
                     SmtpPassword.Equals(other.SmtpPassword)
                 ) &&
                 (
                     FromAddress == other.FromAddress ||
                     FromAddress != null &&
                     FromAddress.Equals(other.FromAddress)
                 ) &&
                 (
                     SmtpSsl == other.SmtpSsl ||
                     SmtpSsl != null &&
                     SmtpSsl.Equals(other.SmtpSsl)
                 ) &&
                 (
                     SmtpStarttls == other.SmtpStarttls ||
                     SmtpStarttls != null &&
                     SmtpStarttls.Equals(other.SmtpStarttls)
                 ) &&
                 (
                     DebugEmail == other.DebugEmail ||
                     DebugEmail != null &&
                     DebugEmail.Equals(other.DebugEmail)
                 ));
        }
Example #3
0
        private MailVerifyResult Send(string templateName)
        {
            try
            {
                MailVerifyResult result = verifier.Verify(this);

                if (result != MailVerifyResult.Ok)
                {
                    return(result);
                }

                string subject__1 = Subject;
                string body__2    = Body;

                if (!string.IsNullOrEmpty(templateName))
                {
                    Subject = Templates[templateName].Subject;
                    Body    = Templates[templateName].Body;
                }

                if (SmtpPort == 0)
                {
                    SmtpPort = 25;
                }

                mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"]       = SmtpServer;
                mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = (SmtpAuthenticate ? 1 : 0);
                mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"]     = SmtpUserId;
                mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"]     = SmtpPassword;
                mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"]   = SmtpPort;
                mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"]       = SmtpUseSsl.ToString().ToLower();
                mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"]        = 2;


                mail.Priority     = MailPriority.Normal;
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.BodyFormat   = MailFormat.Html;

                SmtpMail.SmtpServer = (SmtpServer + ":") + SmtpPort.ToString();
                SmtpMail.Send(mail);

                Subject = subject__1;
                Body    = body__2;
            }
            catch (Exception ex)
            {
                Log.Write(this.cxt, ex);
            }

            return(MailVerifyResult.Ok);
        }
        private void SendMail(string subject, string body, List <MailRecipient> recipients)
        {
            var time             = DateTime.Now.Ticks;
            var resultCollection = new ConcurrentBag <string>();

            var bodypart = body.Length > MaxBodyLengthForLogging?body.Substring(0, MaxBodyLengthForLogging - 3) + "..." : body;

            var logMessage = $"Mail Id {time}" + Environment.NewLine +
                             $"Send mail to {string.Join(", ", recipients.Select(x => x.ToAddr))}" + Environment.NewLine +
                             $"concerning {subject}" + Environment.NewLine +
                             $"containing {bodypart}" + Environment.NewLine + Environment.NewLine;

            Parallel.ForEach(recipients,
                             recipient =>
            {
                try
                {
                    var mail = new MailMessage(recipient.FromAddr, recipient.ToAddr)
                    {
                        Subject      = recipient.Subject,
                        Body         = body,
                        BodyEncoding = Encoding.UTF8,
                        DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure,
                    };

                    var client = new SmtpClient
                    {
                        Port                  = SmtpPort.GetTOrDefault(DefaultSmtpPort),
                        DeliveryMethod        = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = false,
                        Credentials           = new NetworkCredential(SmtpLogin, SmtpPassword),
                        Host                  = SmtpHost
                    };

                    client.Send(mail);

                    var succMsg = $"Sent Email '{recipient.Subject}' from '{recipient.FromAddr}' to '{recipient.ToAddr}' successfully.";
                    resultCollection.Add(succMsg);
                }
                catch (Exception ex)
                {
                    var failMsg = $"Sending Email '{recipient.Subject}' from '{recipient.FromAddr}' to '{recipient.ToAddr}' failed: {ex.Messages()}";
                    resultCollection.Add(failMsg);
                }
            }
                             );
            this.Logger().Debug(logMessage + string.Join(Environment.NewLine, resultCollection));
        }
 /// <summary>
 /// Gets the hash code
 /// </summary>
 /// <returns>Hash code</returns>
 public override int GetHashCode()
 {
     unchecked // Overflow is fine, just wrap
     {
         var hashCode = 41;
         // Suitable nullity checks etc, of course :)
         if (SmtpHost != null)
         {
             hashCode = hashCode * 59 + SmtpHost.GetHashCode();
         }
         if (SmtpPort != null)
         {
             hashCode = hashCode * 59 + SmtpPort.GetHashCode();
         }
         if (SmtpUser != null)
         {
             hashCode = hashCode * 59 + SmtpUser.GetHashCode();
         }
         if (SmtpPassword != null)
         {
             hashCode = hashCode * 59 + SmtpPassword.GetHashCode();
         }
         if (FromAddress != null)
         {
             hashCode = hashCode * 59 + FromAddress.GetHashCode();
         }
         if (SmtpSsl != null)
         {
             hashCode = hashCode * 59 + SmtpSsl.GetHashCode();
         }
         if (SmtpStarttls != null)
         {
             hashCode = hashCode * 59 + SmtpStarttls.GetHashCode();
         }
         if (DebugEmail != null)
         {
             hashCode = hashCode * 59 + DebugEmail.GetHashCode();
         }
         return(hashCode);
     }
 }
Example #6
0
        private bool SaveEmailAlertOptions()
        {
            // Validate input.
            if (IsEmailAlertsEnabled.IsChecked == true)
            {
                var regex = new Regex("^\\d+$");

                if (SmtpServer.Text.Length == 0)
                {
                    EmailAlertsTab.Focus();
                    MessageBox.Show(
                        "Please enter a valid address for your outgoing mail server.",
                        "vmPing Error",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error);
                    SmtpServer.Focus();
                    return(false);
                }
                else if (SmtpPort.Text.Length == 0 || !regex.IsMatch(SmtpPort.Text))
                {
                    EmailAlertsTab.Focus();
                    MessageBox.Show(
                        "Please enter a valid port number.  The standard is 25.",
                        "vmPing Error",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error);
                    SmtpPort.Focus();
                    return(false);
                }
                else if (EmailRecipientAddress.Text.Length == 0)
                {
                    EmailAlertsTab.Focus();
                    MessageBox.Show(
                        "Please enter a valid recipient email address.  This is the address that will receive alerts.",
                        "vmPing Error",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error);
                    EmailRecipientAddress.Focus();
                    return(false);
                }
                else if (EmailFromAddress.Text.Length == 0)
                {
                    EmailAlertsTab.Focus();
                    MessageBox.Show(
                        "Please enter a valid 'from' address.  This address will appear as the sender for any alerts that are sent.",
                        "vmPing Error",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error);
                    EmailFromAddress.Focus();
                    return(false);
                }
                if (IsSmtpAuthenticationRequired.IsChecked == true)
                {
                    ApplicationOptions.IsEmailAuthenticationRequired = true;
                    if (SmtpUsername.Text.Length == 0)
                    {
                        EmailAlertsTab.Focus();
                        MessageBox.Show(
                            "Please enter a valid username for authenticating to your mail server.",
                            "vmPing Error",
                            MessageBoxButton.OK,
                            MessageBoxImage.Error);
                        SmtpUsername.Focus();
                        return(false);
                    }
                }
                else
                {
                    ApplicationOptions.IsEmailAuthenticationRequired = false;
                    SmtpUsername.Text     = string.Empty;
                    SmtpPassword.Password = string.Empty;
                }

                ApplicationOptions.IsEmailAlertEnabled = true;
                ApplicationOptions.EmailServer         = SmtpServer.Text;
                ApplicationOptions.EmailPort           = SmtpPort.Text;
                ApplicationOptions.EmailUser           = SmtpUsername.Text;
                ApplicationOptions.EmailPassword       = SmtpPassword.Password;
                ApplicationOptions.EmailRecipient      = EmailRecipientAddress.Text;
                ApplicationOptions.EmailFromAddress    = EmailFromAddress.Text;

                if (IsSmtpAuthenticationRequired.IsChecked == true && SaveAsDefaults.IsChecked == true)
                {
                    MessageBox.Show(
                        "You have chosen to save your SMTP credentials to disk." + Environment.NewLine + Environment.NewLine +
                        "While the data is stored in an encrypted format, anyone with access to your vmPing configuration file can decrypt the data.",
                        "vmPing Warning",
                        MessageBoxButton.OK,
                        MessageBoxImage.Warning);
                }

                return(true);
            }
            else
            {
                ApplicationOptions.IsEmailAlertEnabled = false;
                return(true);
            }
        }
Example #7
0
        private bool SaveEmailAlertOptions()
        {
            // Validate input.
            if (IsEmailAlertsEnabled.IsChecked == true)
            {
                var regex = new Regex("^\\d+$");

                if (SmtpServer.Text.Length == 0)
                {
                    EmailAlertsTab.Focus();
                    MessageBox.Show(
                        "Please enter a valid address for your outgoing mail server.",
                        "vmPing Error",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error);
                    SmtpServer.Focus();
                    return(false);
                }
                else if (SmtpPort.Text.Length == 0 || !regex.IsMatch(SmtpPort.Text))
                {
                    EmailAlertsTab.Focus();
                    MessageBox.Show(
                        "Please enter a valid port number.  The standard is 25.",
                        "vmPing Error",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error);
                    SmtpPort.Focus();
                    return(false);
                }
                else if (EmailRecipientAddress.Text.Length == 0)
                {
                    EmailAlertsTab.Focus();
                    MessageBox.Show(
                        "Please enter a valid recipient email address.  This is the address that will receive alerts.",
                        "vmPing Error",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error);
                    EmailRecipientAddress.Focus();
                    return(false);
                }
                else if (EmailFromAddress.Text.Length == 0)
                {
                    EmailAlertsTab.Focus();
                    MessageBox.Show(
                        "Please enter a valid 'from' address.  This address will appear as the sender for any alerts that are sent.",
                        "vmPing Error",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error);
                    EmailFromAddress.Focus();
                    return(false);
                }
                if (IsSmtpAuthenticationRequired.IsChecked == true)
                {
                    ApplicationOptions.IsEmailAuthenticationRequired = true;
                    if (SmtpUsername.Text.Length == 0)
                    {
                        EmailAlertsTab.Focus();
                        MessageBox.Show(
                            "Please enter a valid username for authenticating to your mail server.",
                            "vmPing Error",
                            MessageBoxButton.OK,
                            MessageBoxImage.Error);
                        SmtpUsername.Focus();
                        return(false);
                    }
                }

                ApplicationOptions.IsEmailAlertEnabled = true;
                ApplicationOptions.EmailServer         = SmtpServer.Text;
                ApplicationOptions.EmailPort           = SmtpPort.Text;
                ApplicationOptions.EmailUser           = SmtpUsername.Text;
                ApplicationOptions.EmailPassword       = SmtpPassword.Password;
                ApplicationOptions.EmailRecipient      = EmailRecipientAddress.Text;
                ApplicationOptions.EmailFromAddress    = EmailFromAddress.Text;

                return(true);
            }
            else
            {
                return(true);
            }
        }