Ejemplo n.º 1
0
        public PostViewModel GetPost(string postId)
        {
            var post = _dataProvider.GetPostById(int.Parse(postId));

            if (post == null)
            {
                return(null);
            }
            return(_mapper.Map <PostViewModel>(post));
        }
Ejemplo n.º 2
0
        public async Task <bool> SendCommentEmail(CommentCreateRequest request)
        {
            var post = _dataProvider.GetPostById(request.PostId);
            var user = _dataProvider.GetUserById(post.UserId);
            var blog = _dataProvider.GetBlog();

            if (!string.IsNullOrWhiteSpace(blog.SmtpPassword))
            {
                blog.SmtpPassword = _protector.Unprotect(blog.SmtpPassword);
            }

            var response = true;

            try
            {
                using (var client = new SmtpClient
                {
                    Credentials = new NetworkCredential(blog.SmtpUsername, blog.SmtpPassword),
                    EnableSsl = blog.SmtpUseSsl,
                    Host = blog.SmtpHost,
                    Port = int.Parse(blog.SmtpPort),
                    Timeout = 10000
                })
                {
                    var sendMailTask = client.SendMailAsync(user.Email, user.Email,
                                                            $"{blog.EmailPrefix} Comment on post '{post.Title}'",
                                                            $"Comment by: {request.AuthorName} - {request.AuthorEmail}\n" +
                                                            $"Comment: {request.Text}");

                    if (await Task.WhenAny(sendMailTask, Task.Delay(5000)) == sendMailTask)
                    {
                        response = true;
                    }
                    else
                    {
                        // sendMailTask task timed out
                        client.SendAsyncCancel();
                        response = false;
                    }
                }
            }
            catch
            {
                response = false;
            }
            return(response);
        }