Exemplo n.º 1
0
        public void SendEmail(EmailLetter letter)
        {
            if (letter == null || String.IsNullOrEmpty(letter.Receiver))
            {
                throw new ArgumentException("Wrong letter information.");
            }

            using (var message = new MailMessage(_adminMail, letter.Receiver))
            {
                message.Subject = letter.Subject;
                message.Body    = letter.Message;

                using (SmtpClient client = new SmtpClient
                {
                    EnableSsl = _enableSsl,
                    Host = _hostName,
                    Port = _port,
                    Credentials = new NetworkCredential(_adminMail, _adminMailPass)
                })
                {
#if (DEBUG)
                    Console.WriteLine("\n" + letter.Subject + "\n" + letter.Message + "\n");
#else
                    client.Send(message);
#endif
                }
            }
        }
        private void DoSendEmail(EmailLetter letter)
        {
            logger.Info("Sending a email message.");

            var message = new MimeMessage();

            message.From.Add(new MailboxAddress("Рикард-Недвижимость", infoOptions.Value.Email.From));
            message.To.Add(new MailboxAddress(letter.To));
            message.Subject = letter.Subject;

            var bodyBuilder = new BodyBuilder();

            bodyBuilder.HtmlBody = letter.Body;
            message.Body         = bodyBuilder.ToMessageBody();

            using (var client = new SmtpClient())
            {
                client.Connect(infoOptions.Value.Email.SmtpAddress, infoOptions.Value.Email.SmtpPort, infoOptions.Value.Email.EnableSsl);
                client.AuthenticationMechanisms.Remove("XOAUTH2");
                client.Authenticate(infoOptions.Value.Email.Username, infoOptions.Value.Email.Password);
                client.Send(message);
                client.Disconnect(true);
            }

            logger.Info("A email message have been sent via smtp");
        }
Exemplo n.º 3
0
        public async void SendEmailAsync(EmailLetter emailLetter)
        {
            var host = _configuration["Mail:SMTP:SMTPServer"];

            var port = 0;

            try
            {
                port = int.Parse(_configuration["Mail:SMTP:Port"]);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            var useTls   = _configuration["Mail:SMTP:UseTLS"] == "true";
            var userName = _configuration["Mail:SMTP:Username"];
            var password = _configuration["Mail:SMTP:Password"];
            var domain   = _configuration["Mail:SMTP:Domain"];

            if (host == "" || port == 0 || userName == "" || password == "" || domain == "")
            {
                return;
            }

            var emailMessage = new MimeMessage();

            emailMessage.From.Add(new MailboxAddress("Derogation system Bot", "*****@*****.**"));
            emailLetter.Recipients.ForEach(user => emailMessage.To.Add(new MailboxAddress(user.DerogationUser, user.UserMailBase + "@" + domain)));
            emailMessage.Subject = emailLetter.Subject;
            emailMessage.Body    = new TextPart(TextFormat.Html)
            {
                Text = emailLetter.Body
            };

            using var client = new SmtpClient
                  {
                      ServerCertificateValidationCallback = (s, c, h, e) => true
                  };


            await client.ConnectAsync(host, port, useTls?SecureSocketOptions.StartTls : SecureSocketOptions.None);

            await client.AuthenticateAsync(userName, password);

            await client.SendAsync(emailMessage);

            await client.DisconnectAsync(true);
        }
Exemplo n.º 4
0
        public void SendEmail(User user)
        {
            IEmail email = new EmailService();

            EmailLetter letter = new EmailLetter();

            letter.Subject  = $"Happy birthday {user.FirstName} {user.LastName}!";
            letter.Message  = "Best wishes for a joyous day filled with love and laughter.";
            letter.Receiver = user.Email;

            try
            {
                email.SendEmail(letter);
            }
            catch (ArgumentException ex) { Console.WriteLine(ex.Message); }
        }
 public void Send(EmailLetter letter) => Worker.AddData(letter);