我有一个问题与联系表单.在本地主机上工作.当部署时,它给出错误时,试图通过提交按钮发布它



我在我的Godaddy托管Asp.net core 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页面的控制器(删除了电子邮件和密码):

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");
}
}

它在本地主机上工作得很好,但是一旦发布到服务器就不会提交电子邮件。任何帮助或建议将不胜感激。这几天来,我一直在寻找、寻找并试图解决这个问题。

好了,我算出来了。首先,GoDaddy不允许外部服务器加入他们的共享主机计划。因此,在改变使用他们的relay-hosting.secureserver.net smtp服务器后,我仍然收到错误。我最终通过将捕获(Exception ex)中捕获的视图数据传递给Contact Us页面的方式将Exception错误发送到生产页面,这样我就可以进行故障排除。我注意到,在这一点上,它没有更新我的页面视图的内容。经过一些其他的检查,我发现在发布时,我的视图和模型没有被更新。在多次尝试强迫它进行更改之后,最终成功的是将发布配置从发布更改为调试、上传,然后再更改回发布并再次上传。从这里开始,这就是有效的代码注意如果从与电子邮件关联的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的模型。它被命名为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; }
}
}

相关内容

最新更新