public CakeMailResult SendEmail(string senderName, string senderAddress, string recipientAddress, string subject, string htmlContent, string textContent, CakeMailSettings settings)
        {
            var recipients = new[] { recipientAddress };

            return(SendEmail(senderName, senderAddress, recipients, subject, htmlContent, textContent, settings));
        }
Example #2
0
        public CakeMailResult SendEmail(string senderName, string senderAddress, IEnumerable <string> recipientsAddress, string subject, string htmlContent, string textContent, CakeMailSettings settings)
        {
            try
            {
                if (settings == null)
                {
                    throw new ArgumentNullException(nameof(settings));
                }

                if (string.IsNullOrWhiteSpace(senderAddress))
                {
                    throw new ArgumentNullException(nameof(senderAddress), "You must specify the 'from' email address.");
                }

                if (string.IsNullOrWhiteSpace(subject))
                {
                    throw new ArgumentNullException(nameof(subject), "You must specify the subject.");
                }

                if (string.IsNullOrWhiteSpace(htmlContent) && string.IsNullOrEmpty(textContent))
                {
                    throw new ArgumentException("You must specify the HTML content and/or the text content. We can't send an empty email.");
                }

                if (recipientsAddress == null || !recipientsAddress.Any(r => !string.IsNullOrWhiteSpace(r)))
                {
                    throw new ArgumentNullException(nameof(recipientsAddress), "You must specify at least one recipient.");
                }

                var safeRecipients = recipientsAddress.Where(r => !string.IsNullOrEmpty(r));
                if (!safeRecipients.Any())
                {
                    throw new CakeException("None of the recipients you specified have an email address");
                }

                var client = new CakeMailRestClient(settings.ApiKey);
                _context.Log.Verbose("Sending email to {0} via the CakeMail API...", string.Join(", ", safeRecipients.ToArray()));

                var loginInfo = client.Users.LoginAsync(settings.UserName, settings.Password).Result;
                var userKey   = loginInfo.UserKey;

                // CakeMail does not support sending a relay to multiple recipients, therefore we must send one email to each recipient
                foreach (var recipientAddres in safeRecipients)
                {
                    client.Relays.SendWithoutTrackingAsync(userKey, recipientAddres, subject, htmlContent, textContent, senderAddress, senderName).Wait();
                }

                return(new CakeMailResult(true, DateTime.UtcNow.ToString("u"), string.Empty));
            }
            catch (Exception e)
            {
                while (e.InnerException != null)
                {
                    e = e.InnerException;
                }

                if (settings.ThrowOnFail.HasValue && settings.ThrowOnFail.Value)
                {
                    throw new CakeException(e.Message);
                }
                else
                {
                    return(new CakeMailResult(false, DateTime.UtcNow.ToString("u"), e.Message));
                }
            }
        }
        public CakeMailResult SendEmail(string senderName, string senderAddress, IEnumerable <string> recipients, string subject, string htmlContent, string textContent, CakeMailSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (string.IsNullOrWhiteSpace(senderAddress))
            {
                throw new ArgumentNullException(nameof(senderAddress), "You must specify the 'from' email address.");
            }

            if (recipients == null || !recipients.Any(r => !string.IsNullOrWhiteSpace(r)))
            {
                throw new ArgumentNullException(nameof(recipients), "You must specify at least one 'to' email address.");
            }

            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentNullException(nameof(subject), "You must specify the subject.");
            }

            if (string.IsNullOrWhiteSpace(htmlContent) && string.IsNullOrEmpty(textContent))
            {
                throw new ArgumentException("You must specify the HTML content and/or the text content. We can't send an empty email.");
            }

            return(_context.SendEmail(senderName, senderAddress, recipients, subject, htmlContent, textContent, settings));
        }
Example #4
0
        public CakeMailResult SendEmail(string senderName, string senderAddress, IEnumerable <MailAddress> recipients, string subject, string htmlContent, string textContent, CakeMailSettings settings)
        {
            var recipientsAddress = recipients.Select(r => r.Address).ToArray();

            return(SendEmail(senderName, senderAddress, recipientsAddress, subject, htmlContent, textContent, settings));
        }