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); }
public PostListViewModel GetPostList(PostListRequest request) { var blog = _dataProvider.GetBlog(); var posts = _dataProvider.GetPosts(blog.PostsPerPage, request.PageIndex); return(new PostListViewModel { PageTitle = blog.Name + " - " + blog.Description, PageSize = blog.PostsPerPage, PageIndex = request.PageIndex, Posts = _mapper.Map <List <PostViewModel> >(posts.posts), TotalPosts = posts.totalPosts }); }