コード例 #1
0
        /// <summary>
        /// Create a FORMPOST using the given posted values
        /// https://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-format
        /// </summary>
        /// <param name="formPost"></param>
        /// <returns></returns>
        public override async Task <ActionResult> Create(FormPost formPost)
        {
            // Set formPost attributes where applicable
            formPost.formId           = formPost.id;
            formPost.id               = 0; // Id will be appended
            formPost.authorId         = User.Identity.Name;
            formPost.dateCreated      = DateTime.UtcNow.ToLocalTime();
            formPost.dateLastModified = DateTime.UtcNow.ToLocalTime();
            formPost.resultsToXml();

            // Attempt to save the form to the database
            try {
                // Save the object
                getObjectDbContext().FormPosts.Add(formPost);
                int success = getObjectDbContext().SaveChanges();
                // Return the success response
                return(Json(new Payload(
                                1, "FORMPOST", formPost,
                                formPost.getMessage()
                                )));
            } catch (Exception e) {
                var message = e.Message;
                return(Json(new Payload(0, e, "FormPostController.submit(" + e.GetType() + ")" + formPost.getMessage() + "\n\n" + e.Message)));
            }
        }
コード例 #2
0
        /// <summary>
        /// Sends the FormPost as an email
        /// See http://www.mikesdotnetting.com/article/268/how-to-send-email-in-asp-net-mvc
        /// </summary>
        /// <param name="success"></param>
        /// <param name="formPost"></param>
        private async Task <Boolean> sendEmailAsync(int success, FormPost formPost)
        {
            if (success == 1)
            {
                var body    = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
                var message = new MailMessage();
                message.To.Add(new MailAddress(User.Identity.Name));
                message.From    = new MailAddress(User.Identity.Name);
                message.Subject = "Form ID: " + formPost.id;
                message.Body    = string.Format(
                    body, User.Identity.Name, User.Identity.Name,
                    "Success: " + success + "\n\n" + formPost.getMessage()
                    );
                message.IsBodyHtml = true;

                // Send the email asynchronously
                using (var smtp = new SmtpClient()) {
                    await smtp.SendMailAsync(message);
                }
            }
            return(true);
        }