asp.net mvc 4将表单从站点发送到电子邮件 [英] asp.net mvc 4 Send form from site to email

查看:85
本文介绍了asp.net mvc 4将表单从站点发送到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mvc网站上有反馈表,看起来像

I have feedback form on my mvc site, it looks like

并且我需要将表格发送到电子邮件.

and I need to send my form to email.

我为表单创建了模型

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;

 namespace ComponentTrading.Web.Models
 {
public class FeedbackForm
    {
    public string Name { get; set; } 
    public string Email { get; set; } 
    public string Phone { get; set; }
    public string Message { get; set; }
    }
}

我为表单Contacts.cshtml创建了视图

I created view for my form Contacts.cshtml

 @using (Html.BeginForm("Contacts", "Home", FormMethod.Post, new { id = "contact-form" }))
{
<fieldset>
    @Html.TextBoxFor(model => model.Name, new { @Value = Name })
    @Html.TextBoxFor(model => model.Email, new { @Value = "E-mail" })
    @Html.TextBoxFor(model => model.Phone, new { @Value = Phone })
    @Html.TextAreaFor(model => model.Message, new { @class = "img-shadow" })
    <input class="form-button" data-type="reset" value="Clear" />
    <input class="form-button" data-type="submit" type="submit" value="Send" />
</fieldset>     
}

然后我在控制器上编写了这段代码

And I wrote this code at my controller

    [HttpGet]
    public ActionResult Contacts()
    {
        FeedbackForm temp = new FeedbackForm();
        temp.Message = @Resources.Global.Contacts_Form_Message_Field;
        return View(temp);
    }


    [HttpPost]
    public ActionResult Contacts(FeedbackForm Model)
    {
        string Text = "<html> <head> </head>" +
        " <body style= \" font-size:12px; font-family: Arial\">"+
        Model.Message+
        "</body></html>";

        SendEmail("tayna-anita@mail.ru", Text);
        FeedbackForm tempForm = new FeedbackForm();
        return View(tempForm);
    }


    public static bool SendEmail(string SentTo, string Text)
    {
        MailMessage msg = new MailMessage();

        msg.From = new MailAddress("Test@mail.ru");
        msg.To.Add(SentTo);
        msg.Subject = "Password";
        msg.Body = Text;
        msg.Priority = MailPriority.High;
        msg.IsBodyHtml = true;

        SmtpClient client = new SmtpClient("smtp.mail.ru", 25);



        client.UseDefaultCredentials = false;
        client.EnableSsl = false;
        client.Credentials = new NetworkCredential("TestLogin", "TestPassword");
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        //client.EnableSsl = true;

        try
        {
            client.Send(msg);
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }

我的代码没有任何错误, 但是,当我按发送"按钮时,我没有收到电子邮件,并且我的表格也不清楚.

I dont have any errors at my code, but when I push Send button, I dont get email and my form doesnt become clear.

我使用了Fiddler Web调试器,并点击了发送按钮

I used Fiddler Web Debugger and I got for Send-button click

如我所见,这意味着没事,所以我不能不明白是怎么回事?

As I see, it means all right, so I cant undersnand what's wrong?

推荐答案

我已经复制粘贴了您的代码,并进行了一些小的修改,使它可以正常工作:

I have copy-pasted your code and got it to work, with some small modifications:

  1. 我没有设置Credentials属性
  2. 我修改了模板以删除@Value属性:

  1. I did not set the Credentials property
  2. I modified the template to remove the @Value attribute:

@ Html.TextBoxFor(model => model.Name) @ Html.TextBoxFor(模型=>模型.电子邮件) @ Html.TextBoxFor(model => model.Phone)

@Html.TextBoxFor(model => model.Name) @Html.TextBoxFor(model => model.Email) @Html.TextBoxFor(model => model.Phone)

这些是我所做的唯一更改,我使您的应用程序可以正常工作.这使我相信SMTP客户端的配置中肯定有问题.您可以检查client.Send(msg);引发的异常吗?我希望它会说出有关无效网络凭据的信息或类似内容.

These are the only changes I made and I got your application to work. This leads me to believe that there must be something wrong in your SMTP client's configuration. Can you examine the exception that is thrown by client.Send(msg);? I expect it will say something about invalid network credentials or something like that.

这篇关于asp.net mvc 4将表单从站点发送到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆