如何解决我有联系表格的问题在本地主机上工作部署时尝试通过提交按钮发布时出现错误
我在 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 服务器后,我仍然收到错误消息。我最终通过将 catch(Exception ex) 中捕获的视图数据传递到“联系我们”页面将异常错误发送到生产页面,以便我可以进行故障排除。我当时注意到它没有更新我的页面视图的内容。经过一些其他检查后,我发现在发布时,我的视图和模型没有更新。在多次尝试强制更改后,最终起作用的是将发布配置从发布更改为调试、上传,然后更改回发布并再次上传。从那里这是有效的代码 注意 如果从与电子邮件关联的 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.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."),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; }
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。