public async Task <bool> SendEmailAsync(EmailDetailsDTO emailDetailsDTO)
        {
            try
            {
                EmailSender objEmailSender = new EmailSender();
                bool        result         = await objEmailSender.SendEmailAsync(emailDetailsDTO, _to, _from, _subject, _plainTextMesage, _htmlMessage, _replyTo);

                return(result);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error:" + ex.Message);
                return(false);
            }
        }
Beispiel #2
0
        public async Task <IActionResult> ContactUs(ContactUsViewModel model)
        {
            var contact = new ContactUs();

            contact.FullName = model.FullName;
            contact.Email    = model.Email;
            contact.Phone    = model.Phone;
            contact.Message  = model.Message;

            _db.Contacts.Add(contact);
            _db.SaveChanges();

            EmailHelper helper      = new EmailHelper();
            string      htmlmessage = string.Empty;
            string      subject     = "Contact Us";

            var body = "Hi admin,<br/> <br/> " + model.FullName + " " + "Please reach out him/her.<br>" +
                       "<br> Email: " + model.Email + "<br> Phone: " + model.Phone + "<br/> <b>Message: </b>" + model.Message;

            helper = new EmailHelper(_configuration["EmailSettings:EmailUser"], model.Email.Trim(), subject, null, body, null);


            helper = new EmailHelper(_configuration["EmailSettings:EmailUser"], model.Email.Trim(), subject, null, body, null, null);

            EmailDetailsDTO emailDetails = new EmailDetailsDTO()
            {
                Server   = _configuration.GetValue <string>("EmailSettings:EmailServer") == null ? string.Empty : _configuration.GetValue <string>("EmailSettings:EmailServer"),
                Port     = Convert.ToInt32(_configuration["EmailSettings:EmailPort"]),
                User     = _configuration["EmailSettings:EmailUser"] == null ? string.Empty : _configuration["EmailSettings:EmailUser"].ToString(),
                Password = _configuration["EmailSettings:EmailPassword"] == null ? string.Empty : _configuration["EmailSettings:EmailPassword"].ToString(),
                UseSsl   = Convert.ToBoolean(_configuration["EmailSettings:EmailUseSsl"]),
                RequiresAuthentication = Convert.ToBoolean(_configuration["EmailSettings:EmailRequiresAuthentication"])
            };



            bool result = await Task.Run(() => helper.SendEmailAsync(emailDetails));

            if (result == true)
            {
                return(Ok("EmailSuccessfully"));
            }
            else
            {
                return(Ok("EmailNotSuccessfully"));
            }
        }
        public async Task <bool> SendEmailAsync(EmailDetailsDTO emailDetailsDTO, string to, string from,
                                                string subject, string plainTextMessage, string htmlMessage,
                                                string replyTo = null, string pdf = null)
        {
            bool hasPlainText = !string.IsNullOrWhiteSpace(plainTextMessage);
            bool hasHtml      = !string.IsNullOrWhiteSpace(htmlMessage);
            var  message      = new MimeMessage();

            #region Argument Exceptions
            if (string.IsNullOrWhiteSpace(to))
            {
                throw new ArgumentException("no To address provided");
            }
            if (string.IsNullOrWhiteSpace(from))
            {
                throw new ArgumentException("no from address provided");
            }
            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentException("no subject provided");
            }
            if (string.IsNullOrWhiteSpace(to))
            {
                throw new ArgumentException("no message provided");
            }
            #endregion

            message.From.Add(new MailboxAddress("", from));
            if (!string.IsNullOrWhiteSpace(replyTo))
            {
                message.ReplyTo.Add(new MailboxAddress("", replyTo));
            }
            message.To.Add(new MailboxAddress("", to));
            message.Subject = subject;


            BodyBuilder bodyBuilder = new BodyBuilder();
            if (hasPlainText)
            {
                bodyBuilder.TextBody = plainTextMessage;
            }
            if (hasHtml)
            {
                bodyBuilder.HtmlBody = htmlMessage;
            }

            //bodyBuilder.Attachments.Add(pdf);

            message.Body = bodyBuilder.ToMessageBody();

            try
            {
                using (SmtpClient client = new SmtpClient())
                {
                    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                    await client.ConnectAsync(
                        emailDetailsDTO.Server,
                        emailDetailsDTO.Port,
                        SecureSocketOptions.StartTlsWhenAvailable).ConfigureAwait(true);

                    // XOAUTH2 authentication mechanism.
                    client.AuthenticationMechanisms.Remove("XOAUTH2");

                    if (emailDetailsDTO.RequiresAuthentication)
                    {
                        await client.AuthenticateAsync(emailDetailsDTO.User, emailDetailsDTO.Password)
                        .ConfigureAwait(false);
                    }
                    await client.SendAsync(message).ConfigureAwait(false);

                    await client.DisconnectAsync(true).ConfigureAwait(false);

                    return(true);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }