示例#1
0
        public SmtpClient(SMTPClientConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            Configuration = configuration;
        }
        private async Task <SendEmailResult> SendNotificationEmail(TenantInformation tenant, NotificationMessage message, INotificationRepository repository)
        {
            Logger.Log($"Handling started for user ID {message.UserId} with message {message.ToJSON()}", message, tenant);

            if (message.To == null || !message.To.Any() || message.To.All(string.IsNullOrWhiteSpace))
            {
                Logger.Log("Failed to send email because no recipients were specified", message, tenant, LogLevel.Error);
                return(await Task.FromResult(SendEmailResult.Error));
            }

            Logger.Log("Getting email settings", message, tenant);
            var emailSettings = await repository.GetEmailSettings();

            if (emailSettings == null || string.IsNullOrWhiteSpace(emailSettings.HostName))
            {
                Logger.Log($"Failed to send email because invalid email settings were provided for tenant {tenant.TenantId}", message, tenant, LogLevel.Error);
                return(await Task.FromResult(SendEmailResult.Error));
            }
            Logger.Log($"Email settings found. Host Name: {emailSettings.HostName}. Sender Email Address: {emailSettings.SenderEmailAddress}. User Name: {emailSettings.UserName}", message, tenant);

            // Get the logo
            byte[] logoImageArray = new LogoDataProvider().GetLogo();
            string logoImageSrc   = null;

            if (logoImageArray != null)
            {
                logoImageSrc = GetImageSrc(logoImageArray, DiscussionEmail.LogoImageAttachmentContentId);
            }

            var notificationEmail = new NotificationEmail(
                message.ProjectId,
                message.ProjectName,
                message.ArtifactId,
                message.ArtifactName,
                message.ArtifactUrl,
                message.Message,
                message.Header,
                logoImageSrc,
                message.BlueprintUrl);

            var emailMessage = new Message
            {
                Subject         = message.Subject,
                FromDisplayName = message.From,
                To         = message.To.ToArray(),
                From       = emailSettings.SenderEmailAddress,
                IsBodyHtml = true,
                Body       = new NotificationEmailContent(notificationEmail).TransformText()
            };

            if (logoImageArray != null)
            {
                emailMessage.DiscussionEmail = new DiscussionEmail
                {
                    LogoImageSrc             = logoImageSrc,
                    LogoImageAttachmentArray = logoImageArray
                };
            }

            var smtpClientConfiguration = new SMTPClientConfiguration
            {
                Authenticated = emailSettings.Authenticated,
                EnableSsl     = emailSettings.EnableSSL,
                HostName      = emailSettings.HostName,
                // Bug 61312: Password must be decrypted before saving in configuration
                Password = SystemEncryptions.DecryptFromSilverlight(emailSettings.Password),
                Port     = emailSettings.Port,
                UserName = emailSettings.UserName
            };

            if (string.IsNullOrWhiteSpace(smtpClientConfiguration.HostName))
            {
                Logger.Log("Sending email failed because no host name was found. Check your email settings", message, tenant, LogLevel.Error);
                return(await Task.FromResult(SendEmailResult.Error));
            }

            Logger.Log($"Sending Email to {string.Join(",", emailMessage.To)}", message, tenant);
            repository.SendEmail(smtpClientConfiguration, emailMessage);
            Logger.Log("Email Sent Successfully", message, tenant);
            return(await Task.FromResult(SendEmailResult.Success));
        }
示例#3
0
 public void SendEmail(SMTPClientConfiguration smtpClientConfiguration, Message emailMessage)
 {
     MailBee.Global.LicenseKey = "MN800-02CA3564CA2ACAAECAB17D4ADEC9-145F";
     new SmtpClient(smtpClientConfiguration).SendEmail(emailMessage);
 }