コード例 #1
0
ファイル: Comment.cs プロジェクト: stantoxt/Piranha.vNext
        /// <summary>
        /// Takes care of any mail notifications that should be sent.
        /// </summary>
        private void HandleNotifications()
        {
            if (Config.Comments.NotifyAuthor || Config.Comments.NotifyModerators)
            {
                if (App.Mail != null)
                {
                    using (var api = new Api()) {
                        var post = api.Posts.GetSingle(PostId);

                        if (post != null)
                        {
                            var recipients = new List <Mail.Address>();
                            var mail       = new Mail.Message();

                            if (Hooks.Mail.OnCommentMail != null)
                            {
                                // Generate custom mail
                                mail = Hooks.Mail.OnCommentMail(post, this);
                            }
                            else
                            {
                                // Generate default mail
                                var ui = new Client.Helpers.UIHelper();
                                mail.Subject = "New comment posted on " + post.Title;
                                mail.Body    = String.Format(Mail.Defaults.NewComment,
                                                             ui.GravatarUrl(Email, 80),
                                                             App.Env.AbsoluteUrl(ui.Permalink(post)),
                                                             post.Title,
                                                             Author,
                                                             Created.ToString("yyyy-MM-dd HH:mm:ss"),
                                                             Body.Replace("\n", "<br/>"));
                            }

                            if (Config.Comments.NotifyAuthor && !String.IsNullOrWhiteSpace(post.Author.Email))
                            {
                                // Add author address
                                recipients.Add(new Mail.Address()
                                {
                                    Email = post.Author.Email,
                                    Name  = post.Author.Name
                                });
                            }

                            if (Config.Comments.NotifyModerators && !String.IsNullOrWhiteSpace(Config.Comments.Moderators))
                            {
                                // Add moderator addresses
                                foreach (var moderator in Config.Comments.Moderators.Split(new char[] { ',' }))
                                {
                                    recipients.Add(new Mail.Address()
                                    {
                                        Email = moderator.Trim(),
                                        Name  = moderator.Trim()
                                    });
                                }
                            }

                            // Send mail
                            App.Mail.Send(mail, recipients.ToArray());
                        }
                    }
                }
                else
                {
                    App.Logger.Log(Log.LogLevel.ERROR, "Models.Comment.HandleNotifications: No mail provider configured.");
                }
            }
        }
コード例 #2
0
ファイル: MessageViewModel.cs プロジェクト: ascott18/Mailer
        public void Send(string fromName, string fromEmail)
        {
            if (fromName == "")
            {
                throw new ArgumentException("Must input a From name.");
            }

            if (fromEmail == "")
            {
                throw new ArgumentException("Must input a From email.");
            }

            if (Subject == "")
            {
                throw new ArgumentException("Subject cannot be empty.");
            }

            if (Body == "")
            {
                throw new ArgumentException("Body cannot be empty.");
            }
            if (Recipients.Count == 0)
            {
                throw new ArgumentException("Must specify recipients.");
            }

            MailAddress fromAddress;

            try
            {
                fromAddress = new MailAddress(fromEmail, fromName);
            }
            catch (Exception)
            {
                throw new ArgumentException("From address is not valid.");
            }

            var client = new Client("smtp.mailgun.org", 587)
            {
                // Don't waste your time, spammers. This account has since been deleted
                // after this repo was made public.
                // (The PW had to be in source somewhere so our prof could run the app.
                // Putting it here was the simplest solution, and this repo used to be private anyway).
                // I'm still going to leave somewhat valid looking credentials here
                // so you can waste a little bit of your time when your scraper comes across it.
                // If you were scraping really hard, you could get the old (still invalid) creds from the history.
                UserName = "******",
                Password = "******"
            };

            var message = new Mail.Message(client)
            {
                Subject = Subject,
                Body    = Body,
                From    = fromAddress
            };

            foreach (var rvm in Recipients)
            {
                if (rvm is AddressRecipientViewModel)
                {
                    message.AddRecipient((rvm as AddressRecipientViewModel).Address);
                }
                else if (rvm is MailingListRecipientViewModel)
                {
                    var mailingList = (rvm as MailingListRecipientViewModel).MailingList;

                    if (mailingList is DynamicMailingList)
                    {
                        message.AddRecipient(mailingList as DynamicMailingList);
                    }
                    else
                    {
                        message.AddRecipient(mailingList);
                    }
                }
            }

            foreach (var attachment in Attachments)
            {
                message.AddAttachment(attachment.FullPath);
            }

            Task.Run(() =>
            {
                message.Send();
                MessageBox.Show("Sent!");


                Application.Current.Dispatcher.BeginInvoke(
                    new Action(() => MessagePump.Dispatch(this, "MessageSent")),
                    new object[0]
                    );
            });
        }