Example #1
0
        public async Task <bool> SendAsync(MessagePostViewModel message)
        {
            var mime_message = MimeMessageBuilder(message);

            using var emailClient = new SmtpClient();
            emailClient.Connect(smtpServer, smtpPort, true);

            emailClient.AuthenticationMechanisms.Remove("XOAUTH2");

            emailClient.Authenticate(smtpLogin, smtpPassword);

            try
            {
                await emailClient.SendAsync(mime_message);

                return(true);
            }
            catch (System.Exception)
            {
                return(false);
            }
            finally
            {
                emailClient.Disconnect(true);
            }
        }
Example #2
0
        /// <summary>
        /// Преобразует полученное сообщение в Mime Ссообщение
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        MimeMessage MimeMessageBuilder(MessagePostViewModel message)
        {
            var emailMessage  = new MimeMessage();
            var allRecipients = new List <MailboxAddress>();

            var from = new MailboxAddress("Me", smtpLogin);

            emailMessage.From.Add(from);

            var to = new MailboxAddress("recipient", message.Recipient);

            allRecipients.Add(to);

            if (message.Carbon_copy_recipients != null)
            {
                foreach (string addr in message.Carbon_copy_recipients)
                {
                    allRecipients.Add(new MailboxAddress($"recipient{addr}", addr));
                }
            }
            emailMessage.To.AddRange(allRecipients);

            var bodyBuilder = new BodyBuilder();

            bodyBuilder.TextBody = message.Text;

            emailMessage.Subject = message.Subject;
            emailMessage.Body    = bodyBuilder.ToMessageBody();

            return(emailMessage);
        }
        public async Task <MessageViewModel> PostMessage(MessagePostViewModel value)
        {
            var success = await _emailService.SendAsync(value);

            var msg = new Message
            {
                Recipient = value.Recipient,
                Subject   = value.Subject,
                Text      = value.Text,
                Success   = success,
            };
            await _messageRepository.AddAsync(msg);

            if (value.Carbon_copy_recipients != null)
            {
                foreach (string addr in value.Carbon_copy_recipients)
                {
                    await _messageRepository.AddAsync(
                        new Message
                    {
                        Recipient = addr,
                        Subject   = value.Subject,
                        Text      = value.Text,
                        Success   = success,
                    });
                }
            }

            return(new MessageViewModel
            {
                Recipient = value.Recipient,
                Subject = value.Subject,
                Text = value.Text,
                Carbon_copy_recipients = value.Carbon_copy_recipients,
                Success = success,
            });
        }
Example #4
0
 public JsonResult Post(MessagePostViewModel model)
 {
     // TODO: Implement logic
     return(Json(!ModelState.IsValid ? JsonResponseFactory.SuccessResponse() : JsonResponseFactory.ErrorResponse("Some error")));
 }