예제 #1
0
        public static void PopulateFromRule(
            [NotNull] this MimeMessage mimeMessage,
            [NotNull] ForwardRule forwardRule)
        {
            if (forwardRule == null)
            {
                throw new ArgumentNullException(nameof(forwardRule));
            }
            if (mimeMessage == null)
            {
                throw new ArgumentNullException(nameof(mimeMessage));
            }

            if (!string.IsNullOrWhiteSpace(forwardRule.FromEmail))
            {
                mimeMessage.From.Clear();
                mimeMessage.From.Add(
                    new MailboxAddress(forwardRule.FromEmail, forwardRule.FromEmail));
            }

            if (!string.IsNullOrWhiteSpace(forwardRule.ToEmail))
            {
                mimeMessage.To.Clear();
                mimeMessage.Bcc.Clear();
                mimeMessage.Cc.Clear();
                mimeMessage.To.Add(new MailboxAddress(forwardRule.ToEmail, forwardRule.ToEmail));
            }
        }
예제 #2
0
        public static SmtpClient CreateConnectedSmtpClient([NotNull] this ForwardRule forwardRule)
        {
            if (forwardRule == null)
            {
                throw new ArgumentNullException(nameof(forwardRule));
            }

            var client = new SmtpClient();

            client.Connect(forwardRule.SMTPServer, forwardRule.SMTPPort, forwardRule.SmtpUseSSL);

            // Note: since we don't have an OAuth2 token, disable
            // the XOAUTH2 authentication mechanism.
            client.AuthenticationMechanisms.Remove("XOAUTH2");

            if (!string.IsNullOrWhiteSpace(forwardRule.SMTPPassword) &&
                !string.IsNullOrWhiteSpace(forwardRule.SMTPUsername))
            {
                // Note: only needed if the SMTP server requires authentication
                client.Authenticate(forwardRule.SMTPUsername, forwardRule.SMTPPassword);
            }

            return(client);
        }