簡體   English   中英

我對聯系表有疑問。 在本地主機上工作。 當嘗試通過提交按鈕發布它時部署它的錯誤

[英]I have an issue with a contact form. Works on localhost. When deployed its giving error when attempting to post it via the submit button

我在 Godaddy 托管的 Asp.net 核心 2.0 Razor 頁面網站上有一個簡單的聯系表格。 在本地主機上一切正常。 部署網站后,如果單擊提交按鈕,則會收到錯誤 500“無法處理請求”。 下面是頁面的代碼:

<form method="post">
    <div class="row gtr-uniform gtr-50">
         <div class="col-6 col-12-mobilep">
             <input  asp-for="Sendmail.FName" placeholder="First Name"/>
             <span asp-validation-for="Sendmail.FName" style="color:red;"></span>
         </div>
         <div class="col-6 col-12-mobilep">
             <input  asp-for="Sendmail.LName" placeholder="Last Name" />
             <span asp-validation-for="Sendmail.LName" style="color:red;"></span>
         </div>
         <div class="col-6 col-12-mobilep">
             <input asp-for="Sendmail.PhoneNumber" placeholder="Phone Number" />
             <span asp-validation-for="Sendmail.PhoneNumber" style="color:red;"></span>
         </div>
         <div class="col-12">
             <select asp-for="Sendmail.BestTimeToCall">
                 <option value="">- Best Time To Call-</option>
                 <option value="Morning">Morning</option>
                 <option value="Afternoon">Afternoon</option>
                 <option value="Evening">Evening</option>
             </select>
             <span asp-validation-for="Sendmail.BestTimeToCall" style="color:red;"></span>
         </div>            
         <div class="col-6 col-12-mobilep">
                <input asp-for="Sendmail.EmailAddress" placeholder="Email" />
                <span asp-validation-for="Sendmail.EmailAddress" style="color:red;"></span>
            </div>
            <div class="col-12">
                <select asp-for="Sendmail.PreferredContactMethod">
                    <option value="">- Preferred Contact Method -</option>
                    <option value="Email">Email</option>
                    <option value="Phone">Phone Call</option>
                </select>
                <span asp-validation-for="Sendmail.PreferredContactMethod" style="color:red;"></span>
            </div>
            <div class="col-12">
                <textarea asp-for="Sendmail.MessageBody" placeholder="Enter your message" rows="6"></textarea>
                <span asp-validation-for="Sendmail.MessageBody" style="color:red;"></span>
            </div>
            <div class="col-12">
                <ul class="actions">
                    <li><button asp-page-handler="ContactUs" class="primary" id="contactsubmitbutton">Submit</button></li>
                   
                    <li>@ViewBag.Message</li>
                </ul>
            </div>
        </div>
    </form>

以下是 ContactUs 頁面的 controller(已刪除電子郵件和密碼):

 public Email Sendmail { get; set; }
        public async Task OnPost()
        {
            try 
            {
                if (ModelState.IsValid)
                {
                        //this is the email address you want the contact form to be sent to. example contactrequestform@thevillagementalhealthgroup.com
                        string To = "email@gmail.com";
                        //this is what the subject line of the email will say.  use a subject line that will be easy to identify
                        string Subject = "New Contact Request From The Village Mental Health Group's Contact Us Form";
                        //this is the body of the email
                        string Body = Sendmail.Body;
                        MailMessage mm = new MailMessage();
                        //this is the to section of the email
                        mm.To.Add(To);
                        //this is the subject section of the email
                        mm.Subject = Subject;
                        //this is what the body of the email message will contain.  Currently it has the input values from the contact form in a readable format
                        mm.Body = "You have a new message from your contact us form on TheVillageMentalHealthgroup.com. Their information is as follows: First Name: " + Sendmail.FName +  " Last Name: " + Sendmail.LName + " Phone Number: " + Sendmail.PhoneNumber + " Best Time To Call: " + Sendmail.BestTimeToCall + " Email Address: " + Sendmail.EmailAddress + " Preferred contact Method: " + Sendmail.PreferredContactMethod + " Their message is as follows: " + Sendmail.MessageBody;
                        //this signifies if the email body is html format.  if you change to yes you can format the email message with html mark up
                        mm.IsBodyHtml = false;
                        //this is where the email is being sent from.  this should be set to the email address specific to the contact form so you will always automatically know it is a contact request from said website
                        mm.From = new MailAddress("email@gmail.com");
                        //this is the email providers smtp address
                        SmtpClient smtp = new SmtpClient("smtp.gmail.com");
                        smtp.Port = 587;
                        smtp.UseDefaultCredentials = true;
                        smtp.EnableSsl = true;
                        //these are user names and password for the email provider
                        smtp.Credentials = new System.Net.NetworkCredential("email@gmail.com", "password");
                        await smtp.SendMailAsync(mm);
                        //this is the message displayed next to the submit button showing successful submission of of contact request
                        ViewData["Message"] = "Thank You. Your has been sent successfully.We will be in contact with as soon as we are available.";
                }
                else
                {
                    //this is the message displayed if there is an issue submitting the form
                    ViewData["Message"] = "There is an issue with the Contact Form. Please contact us by phone or email.";
                }

            }
            catch (Exception ex)
            {
                ViewData["Message"] = ex;
                Response.Redirect("ContactFormError");
            }

        }

它在本地主機上工作正常,但一旦發布到服務器就不會提交 email。 任何幫助或建議將不勝感激。 幾天來,我一直在尋找和尋找並試圖解決這個問題。

好的,所以我想通了。 首先,GoDaddy 不允許在其共享托管計划中使用外部服務器。 因此,在更改為使用他們的 relay-hosting.secureserver.net smtp 服務器后,我仍然收到錯誤消息。 我最終通過將 catch(Exception ex) 中捕獲的視圖數據傳遞給“聯系我們”頁面,將異常錯誤發送到生產頁面,這樣我就可以排除故障了。 我注意到當時它沒有更新我的頁面視圖的內容。 經過其他一些檢查后,我發現在發布時,我的視圖和模型沒有更新。 在多次嘗試強制它更改之后,最終的工作是將發布配置從發布更改為調試、上傳,然后再更改回發布並再次上傳。 從那里開始,如果從與 email 關聯的 Godaddy 托管帳戶進行托管,則不需要使用注釋憑據的代碼。

        public async Task OnPost()
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //this is the email address you want the contact form to be sent to. example email@email.com
                    string To = "destination@email.com";
                    
                    //this is what the subject line of the email will say.  use a subject line that will be easy to identify
                    string Subject = "New Contact Request From ...";
                    //this is the body of the email
                    string Body = Sendmail.Body;
                    MailMessage mm = new MailMessage();
                    //this is the to section of the email
                    mm.To.Add(To);
                    //this is the subject section of the email
                    mm.Subject = Subject;
                    //this is what the body of the email message will contain.  Currently it has the input values from the contact form in a readable format
                    mm.Body = "You have a new message from your contact us form on yoursite.com. </br>" +
                        " Their information is as follows:" +
                        " </br> First Name: " + Sendmail.FName + 
                        " </br> Last Name: " + Sendmail.LName + 
                        " </br> Phone Number: " + Sendmail.PhoneNumber + 
                        " </br> Best Time To Call: " + Sendmail.BestTimeToCall + 
                        " </br> Email Address: " + Sendmail.EmailAddress +
                        " </br> Preferred contact Method: " + Sendmail.PreferredContactMethod + 
                        " </br> Their message is as follows: " + Sendmail.MessageBody;
                    //this signifies if the email body is html format.  if you change to yes you can format the email message with html mark up
                    mm.IsBodyHtml = true;
                    //this is where the email is being sent from.  this should be set to the email address specific to the contact form so you will always automatically know it is a contact request from said website
                    mm.From = new MailAddress("godaddyhostedemail@domain.com");
                    //this is the email providers smtp address
                    SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net");
                    smtp.Port = 25;
                    smtp.UseDefaultCredentials = false;
                    smtp.EnableSsl = true;
                    await smtp.SendMailAsync(mm);
                    //this is the message displayed next to the submit button showing successful submission of of contact request
                    ViewData["Message"] = "Thank You. Your has been sent successfully. We will be in contact with as soon as we are available.";
                }
                else
                {
                    //this is the message displayed if there is an issue submitting the form
                    ViewData["Message"] = "There is an issue with the Contact Form. Please contact us by phone or email.";
                }

            }
            catch (Exception ex)
            {
                ViewData["Message"] = ex;
                //Response.Redirect("ContactFormError");
            }

        }

下面是 Email 的 model。 它被命名為 Email.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace YourNamespaceHere.Models
{
    public class Email
    {
        public string To { get; set; }
        public string From { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }

        [Required(ErrorMessage ="Your first name is required."), MinLength(2), MaxLength(50), Display(Name = "First Name")]
        public string FName { get; set; }

        [Required(ErrorMessage = "Your last name is required."), MinLength(2), MaxLength(50), Display(Name = "Last Name")]
        public string LName { get; set; }

        [Required(ErrorMessage ="Your phone number is required")]
        [DataType(DataType.PhoneNumber)]
        [Display(Name = "Phone Number")]
        [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Please enter a valid phone number.  Must be all numbers no spaces or other characters.")]
        public string PhoneNumber { get; set; }

        [Required(ErrorMessage ="Please let us know the best time to contact you."), Display(Name = "Select the best time frame for us to contact you.")]
        public string BestTimeToCall { get; set; }
        [DataType(DataType.EmailAddress), Required(ErrorMessage ="A valid email address is required"), Display(Name = "Email Address")]
        public string EmailAddress { get; set; }
        [ Required(ErrorMessage ="Please let us know your required contact method"), Display(Name = "Select your preferred method for us to contact you.")]
        public string PreferredContactMethod { get; set; }
        [ Required(ErrorMessage ="Please check the box to prove you are human"), Display(Name = "Check to box to prove you are human")]
        public bool Human { get; set; }
        [Required(ErrorMessage ="Please let us know how we can assist you."), MinLength(4), MaxLength(500), Display(Name = "Enter your message here.")]
        public string MessageBody { get; set; }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM