Example #1
0
        /// <summary>
        /// SendEmailAsync
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <SendMailResponse> SendEmailAsync(ISendMailRequest request)
        {
            var response = new SendMailResponse();

            try
            {
                if (_client == null)
                {
                    _client = new SendGridClient(_clientConfig.ApiKey);
                }

                var recipientList = request.Recipients.Select(recipient => new EmailAddress(recipient)).ToList();

                var from             = new EmailAddress(request.From, request.FromName);
                var plainTextContent = HtmlText.ConvertHtml(request.Message);
                var htmlContent      = request.Message;
                var msg = MailHelper.CreateSingleEmailToMultipleRecipients(from, recipientList, request.Subject, plainTextContent, htmlContent);
                await _client.SendEmailAsync(msg);
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
                response.IsSuccess    = false;
            }


            return(response);
        }
Example #2
0
        /// <summary>
        /// BuildEmailMessage
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private Message BuildEmailMessage(ISendMailRequest model)
        {
            Message message = new Message();

            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            mail.Subject = model.Subject;
            mail.To.Add(new System.Net.Mail.MailAddress(model.Recipients[0]));

            var htmlMimeType = new ContentType("text/html");
            var textMimeType = new ContentType("text/plain");

            // Add text/plain as an AlternativeView
            var plainText     = HtmlText.ConvertHtml(model.Message);
            var plainTextView = AlternateView.CreateAlternateViewFromString(plainText);

            plainTextView.ContentType = textMimeType;
            mail.AlternateViews.Add(plainTextView);

            // Add text/html as an AlternateView
            var htmlView = AlternateView.CreateAlternateViewFromString(model.Message);

            htmlView.ContentType = htmlMimeType;
            mail.AlternateViews.Add(htmlView);

            foreach (var header in model.Headers)
            {
                mail.Headers.Add(header.Key, header.Value);
            }

            MimeKit.MimeMessage mimeMessage = MimeKit.MimeMessage.CreateFromMailMessage(mail);


            message.Raw = Encode(mimeMessage.ToString());

            return(message);
        }
Example #3
0
        /// <summary>
        /// SendEmailAsync
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Task <SendMailResponse> SendEmailAsync(ISendMailRequest request)
        {
            var sendMailResponse = new SendMailResponse();

            var tcs = new TaskCompletionSource <SendMailResponse>();

            //track the message within the handler.
            var sendGuid = Guid.NewGuid();

            SendCompletedEventHandler handler = null;

            handler = (o, ea) =>
            {
                if (!(ea.UserState is Guid) || ((Guid)ea.UserState) != sendGuid)
                {
                    return;
                }
                _client.SendCompleted -= handler;
                if (ea.Cancelled)
                {
                    tcs.SetCanceled();
                }
                else if (ea.Error != null)
                {
                    sendMailResponse.ErrorMessage = ea.Error.Message;
                    tcs.SetResult(sendMailResponse);
                }
                else
                {
                    sendMailResponse.IsSuccess = true;
                    tcs.SetResult(sendMailResponse);
                }
            };

            var msg = new MailMessage(request.From, request.Recipients.First())
            {
                From    = new MailAddress(request.From, request.FromName),
                Subject = request.Subject
            };

            var htmlMimeType = new ContentType("text/html");
            var textMimeType = new ContentType("text/plain");

            // Add text/plain as an AlternativeView
            var plainText     = HtmlText.ConvertHtml(request.Message);
            var plainTextView = AlternateView.CreateAlternateViewFromString(plainText);

            plainTextView.ContentType = textMimeType;
            msg.AlternateViews.Add(plainTextView);

            // Add text/html as an AlternateView
            var htmlView = AlternateView.CreateAlternateViewFromString(request.Message);

            htmlView.ContentType = htmlMimeType;
            msg.AlternateViews.Add(htmlView);

            foreach (var header in request.Headers)
            {
                msg.Headers.Add(header.Key, header.Value);
            }

            _client.SendCompleted += handler;
            _client.SendAsync(msg, sendGuid);

            return(tcs.Task);
        }