Esempio n. 1
0
        public override async Task <SmtpSettingsMessage> GetSmtpSettings(Empty request, ServerCallContext context)
        {
            SmtpCredentials?credentials = await _smtpCredentialManager.FetchCredentialsAsync();

            if (credentials != null)
            {
                return(new SmtpSettingsMessage
                {
                    Hostname = credentials.Hostname,
                    Password = credentials.Password,
                    Port = credentials.Port,
                    SenderAddress = credentials.SenderAddress,
                    Username = credentials.Username,
                });
            }

            return(new SmtpSettingsMessage());
        }
Esempio n. 2
0
        public async Task Execute(
            string email,
            string recipientName,
            string subject,
            string message)
        {
            SmtpCredentials?credentials = await _smtpCredentialManager.FetchCredentialsAsync();

            if (credentials == null)
            {
                throw new Exception("SMTP credentials are not configured");
            }

            MimeMessage mail = new MimeMessage();

            mail.From.Add(new MailboxAddress("No Reply", credentials.SenderAddress));
            mail.To.Add(new MailboxAddress(recipientName, email));
            mail.Subject = subject;

            BodyBuilder bodyBuilder = new BodyBuilder();

            bodyBuilder.TextBody = message;
            mail.Body            = bodyBuilder.ToMessageBody();

            using (SmtpClient client = new SmtpClient())
            {
                await client.ConnectAsync(
                    credentials.Hostname,
                    credentials.Port
                    );

                await client.AuthenticateAsync(credentials.Username, credentials.Password);

                await client.SendAsync(mail);

                await client.DisconnectAsync(true);
            }
        }