简体   繁体   English

我想通过我的ASP.NET项目发送电子邮件

[英]I want to send email through my ASP.NET project

I have created web form, and now I want this data to be stored in the database and form be emailed to the email. 我已经创建了Web表单,现在我希望将此数据存储在数据库中并将表单通过电子邮件发送到电子邮件。 This is not working, because it consistently tells me that there is a Server error in '/' Application. 这不起作用,因为它始终告诉我“ /”应用程序中存在服务器错误。 Even though I redirect to Thankyou page, and also I have included code for Thank you page. 即使我重定向到Thankyou页面,也包含了Thank you页面的代码。 Please tell me what I am doing wrong. 请告诉我我在做什么错。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Nordstromapp.Models;

    public SupportController(){
        _db = new NordstromEntities();
    }

    //
    // GET: /Form/[HttpGet]
    public ActionResult Create()
    {
        var categoryList = new SelectList(new[] {"How the Site Looks",
            "Product Offered", "How the site works", "General Comment"});
        ViewBag.CategoryList = categoryList;


        var concernList = new SelectList(new[] {"Shoes", "Handbags and accessories", "Beauty and Fragrance", "Sales", 
                                "Designe Collection", 
                                "Kids and Babies' collection", "Home gifts", "Service", "Other"});
        ViewBag.ConcernList = concernList;

        return View();
    }

    [HttpPost]
    public ActionResult Create(Models.Support support)
    {

        if (string.IsNullOrWhiteSpace(support.Question))
        {
            ModelState.AddModelError("Question", "Question is required");
        }
        if (ModelState.IsValid)
        { 
            // save to the database
            return Redirect("Thankyou.cshtml");
        }
            return Create();
        }
    }

View: 视图:

@model Nordstromapp.Models.Support

@{

var Question = Request["Question"];
var First_name = Request["First_name"];
var Last_name = Request["Last_name"];
var Telephone_number = Request["Telephone_number"];
var Email = Request["Email"];
var errorMessage = "";
var debuggingFlag = false;
try {
    // Initialize WebMail helper
    WebMail.SmtpServer = "localhost";
    WebMail.SmtpPort = 21;
    WebMail.UserName = "";
    WebMail.Password = "";
    WebMail.From = "";

    // Send email
    WebMail.Send(to: Email,
        subject: "Help request from" + First_name,
        body: Question
    );
}
catch (Exception ex) {
    errorMessage = ex.Message;
}}

<!DOCTYPE html> <html> <head> <title>Request for Assistance</title></head><br><body><br><p>Sorry to hear that you are having trouble, Mr/Ms <b>@Last_name</b>.</p>
@if(errorMessage == ""){<p>An email message has been sent to our customer service
     department regarding the following problem:</p><p><b>@Question</b></p>}
else{<p><b>The email was <em>not</em> sent.</b></p>
    <p>Please check that the code in the ProcessRequest page has correct settings for the SMTP server name, a user name, 
       a password, and a "from" address. </p>
    if(debuggingFlag){
        <p>The following error was reported:</p><p><em>@errorMessage</em></p> }  } </body>

Right off the bat I see a couple issues with the way you end your action. 马上,我看到您结束动作的方式有几个问题。

  1. You should be doing a return RedirectToAction("Create") at the end or a simple return View() so you can display model errors (if you add the helpers to your view or a validationsummary block). 您应该在最后执行return RedirectToAction("Create")或执行简单的return View()以便显示模型错误(如果将助手添加到视图或validatesummary块中)。

  2. You should not redirect to a .chstml page directly. 您不应直接重定向到.chstml页面。 Instead either return View("ThankYou") or, better yet, redirect to a Get action that displays that page. 而是return View("ThankYou")或者更好的是,重定向到显示该页面的Get操作。 (This is the Post, Redirect, Get pattern to avoid issues of re-POSTing data if a user refreshes the page.) (这是发布,重定向,获取模式,以避免在用户刷新页面时重新发布数据的问题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM