コード例 #1
0
ファイル: EmailBase.cs プロジェクト: spkik/net-blank-app
        public void ThrowIfInvalid()
        {
            Recipients.ThrowIfNullOrEmpty(nameof(Recipients));
            Subject.ThrowIfNullOrEmpty(nameof(Subject));
            ViewPath.ThrowIfNullOrEmpty(nameof(ViewPath));

            foreach (string recipient in Recipients)
            {
                if (!StringHelpers.IsValidEmail(recipient))
                {
                    throw new InvalidOperationException($"Email '{recipient}' is invalid");
                }
            }

            if (Cc.Any())
            {
                foreach (string cc in Cc)
                {
                    if (!StringHelpers.IsValidEmail(cc))
                    {
                        throw new InvalidOperationException($"Email '{cc}' is invalid");
                    }
                }

                Cc = Cc.Where(x => !Recipients.Contains(x)).ToArray();
            }

            if (HiddenCc.Any())
            {
                foreach (string cc in HiddenCc)
                {
                    if (!StringHelpers.IsValidEmail(cc))
                    {
                        throw new InvalidOperationException($"Email '{cc}' is invalid");
                    }
                }

                HiddenCc = HiddenCc
                           .Where(x => !Cc.Contains(x))
                           .Where(x => !Recipients.Contains(x)).ToArray();
            }
        }
コード例 #2
0
ファイル: Email.cs プロジェクト: iturribeitia/GPL
        /// <summary>
        /// Creates a Mail object and sends the email if the SMTPServer variable is not
        /// empty or null
        /// </summary>
        /// <param name="timesToRetry">Number of retries</param>
        /// <param name="retryTimeout">Miliseconds between each retry.</param>
        public void SendEmail(int timesToRetry = 0, int retryTimeout = 5000)
        {
            // Create Mail object
            var oMailMessage = new MailMessage();

            try
            {
                // Set properties needed for the email
                oMailMessage.From = new MailAddress(From);
                //oMailMessage.To.Add(new MailAddress(To));

                var splitChar = To.Contains(",") ? "," : ";";

                var emTo = To.Trim().Split(splitChar.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                if (emTo.Length == 0)
                {
                    throw new ArgumentException("Argument 'To' must be a valid email repository, please review It.");
                }

                foreach (string toAdd in emTo)
                {
                    oMailMessage.To.Add(toAdd);
                }


                if (!string.IsNullOrEmpty(Cc))
                {
                    //oMailMessage.CC.Add(new MailAddress(Cc));

                    splitChar = Cc.Contains(",") ? "," : ";";

                    var emCc = Cc.Trim().Split(splitChar.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                    if (emCc.Length == 0)
                    {
                        throw new ArgumentException("Argument 'Cc' must be a valid email repository, please review It.");
                    }

                    foreach (string CcAdd in emCc)
                    {
                        //oMailMessage.To.Add(CcAdd);
                        oMailMessage.CC.Add(CcAdd);
                    }
                }

                oMailMessage.Subject    = ((string.IsNullOrEmpty(Subject) ? "" : Subject));
                oMailMessage.Body       = ((string.IsNullOrEmpty(Body) ? "" : Body));
                oMailMessage.IsBodyHtml = IsBodyHtml;

                if (Attachments != null && Attachments.Count > 0)
                {
                    foreach (string attachment in Attachments)
                    {
                        var mailAttachment = new Attachment(attachment.Trim());
                        oMailMessage.Attachments.Add(mailAttachment);
                    }
                }
                //Utility.RetryAction(() => _smtpClient.Send(oMailMessage), timesToRetry, retryTimeout);
                Utility.RetryMethod(new Action <MailMessage>(_smtpClient.Send), timesToRetry, retryTimeout, oMailMessage);
            }
            finally
            {
                //destroy the oMailMessage object to release attachements from usage by the process
                oMailMessage.Dispose();
            }
        }