我正在使用.NET MVC向我的工作电子邮件发送联系表单。代码运行没有错误,但电子邮件永远不会到达。它在使用gmail帐户时在我的本地服务器上运行,但在尝试使用我的工作电子邮件时在我的服务器上失败。要暂时解决此问题,我已成功添加.aspx版本的联系表单,它工作正常。有什么想法为什么MVC特别不能在服务器上运行?在此先感谢您的帮助。
using System;
using System.Web.Mvc;
using Eclipse.Models;
using System.Net.Mail;
namespace Eclipse.Controllers
{
public class HomeController : Controller
{
private void SendEmail(string subjectText, string bodyText)
{
MailMessage message = new MailMessage();
message.To.Add("info@emaildomain.com");
message.From = new MailAddress("noreply@emaildomain.com",
"My Company Inc.");
message.Subject = subjectText;
message.Body = bodyText;
message.IsBodyHtml = false;
SmtpClient client = new SmtpClient("mail.emaildomain.com");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Port = 25;
client.EnableSsl = false;
client.Credentials = new System.Net.NetworkCredential("noreply@emaildomain.com",
"password");
client.Send(message);
client.Dispose();
}
[Route("~/Contact")]
public ActionResult Contact()
{
if (TempData["FormMessage"] != null)
{
ViewBag.Message = TempData["FormMessage"].ToString();
TempData.Remove("FormMessage");
}
return View();
}
[Route("~/ResultQuestion")]
[HttpPost]
public ActionResult ResultQuestion(ResultContact form)
{
if (ModelState.IsValid)
{
try
{
System.Text.StringBuilder builder = new System.Text.StringBuilder();
builder.Append("Contact Information\n");
builder.AppendFormat("Contact Name: {0}\n", form.Name);
builder.AppendFormat("Contact Email: {0}\n", form.Email);
builder.AppendFormat("Contact Phone: {0}\n", form.Phone);
builder.AppendFormat("Gender: {0}\n", form.Gender);
builder.AppendFormat("Age: {0}\n", form.Age);
builder.AppendFormat("Event: {0}\n", form.Event);
builder.AppendFormat("Bib: {0}\n", form.Bib);
builder.Append("\nQuestions\n");
builder.Append(form.Question);
SendEmail("Result Question from web site", builder.ToString());
ModelState.Clear();
}
catch (Exception error)
{
ModelState.Clear();
TempData["FormMessage"] = string.Format("We're sorry but an error occurred while submitting your request! {0}",
error.Message);
}
}
TempData["FormMessage"] = "Thank you for contacting us. We will be in contact with you soon.";
return RedirectToAction("Contact");
}
[Route("~/EventInquiry")]
[HttpPost]
public ActionResult EventInquiry(EventContact form)
{
if (ModelState.IsValid)
{
try
{
System.Text.StringBuilder builder = new System.Text.StringBuilder();
builder.Append("Contact Information\n");
builder.AppendFormat("Contact Name: {0}\n", form.Name);
builder.AppendFormat("Contact Email: {0}\n", form.Email);
builder.AppendFormat("Contact Phone: {0}\n", form.Phone);
builder.Append("\nEvent Information\n");
builder.AppendFormat("Event Name: {0}\n", form.Event);
builder.AppendFormat("Event Date: {0}\n", form.Date);
builder.AppendFormat("Location: {0}, {1}\n", form.City, form.State);
builder.AppendFormat("Sport: {0}\n", form.Sport);
builder.AppendFormat("Expected Participants: {0}\n", form.Participants);
builder.Append("\nComments\n");
builder.Append(form.Comments);
SendEmail("Event Inquiry from web site", builder.ToString());
ModelState.Clear();
}
catch (Exception error)
{
ModelState.Clear();
TempData["FormMessage"] = string.Format("We're sorry but an error occurred while submitting your request! {0}",
error.Message);
}
}
TempData["FormMessage"] = "Thank you for contacting us. We will be in contact with you soon.";
return RedirectToAction("Contact");
}
}
}
答案 0 :(得分:0)
可能您的电子邮件服务器阻止访问。要使其与gmail一起使用,您必须更改gmail中的设置以允许应用程序代表该帐户发送。我认为您需要对您的电子邮件服务器执行类似操作,但如果没有更多信息,则很难说。