简体   繁体   English

将电子邮件连接到 asp.net 中的联系页面

[英]Connect email to contact page in asp.net

Perhaps it is an easy question but I am new in developing in c#, asp.net.也许这是一个简单的问题,但我是在 c#、asp.net 中开发的新手。 I am building a website and I have some troubles on the Contact page.我正在建立一个网站,我在联系页面上遇到了一些麻烦。 I've been searching for a long time but I couldn't find a suitabe answer.我已经搜索了很长时间,但找不到合适的答案。 My controller code is the following:我的控制器代码如下:

   namespace SITEEXEMPLO.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string Name, string EmailId, string PhoneNo, string Subject, string Message)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(EmailId);
                mail.From = new MailAddress("xoinas23@gmail.com");
                mail.Subject = Subject;

                string userMessage = "";
                userMessage = "<br/>Name :" + Name;
                userMessage = userMessage + "<br/>Email Id: " + EmailId;
                userMessage = userMessage + "<br/>Phone No: " + PhoneNo;
                userMessage = userMessage + "<br/>Message: " + Message;
                string Body = "Hi, <br/><br/> A new enquiry by user. Detail is as follows:<br/><br/> " + userMessage + "<br/><br/>Thanks";


                mail.Body = Body;
                mail.IsBodyHtml = true;

                SmtpClient smtp = new SmtpClient();
                //SMTP Server Address of gmail
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("xoinas23@gmail.com", "xxxxx");
                // Smtp Email ID and Password For authentication
                smtp.EnableSsl = true;
                smtp.Send(mail);
                ViewBag.Message = "Thank you for contacting us.";
            }
            catch
            {
                ViewBag.Message = "Error............";
            }

            return View();
        }

And the HTML code is the following: HTML代码如下:

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <table width="100%;" border="1">
        <tr>
            <td align="right" colspan="2" style="text-align: center"><strong>SEND EMAIL USING GMAIL ACCOUNT IN MVC</strong></td>
        </tr>
        <tr>
            <td align="right">&nbsp;</td>
            <td align="left">
                @ViewBag.Message
            </td>
        </tr>
        <tr>
            <td align="right">Name :</td>
            <td align="left">
                <input id="txtName" name="Name" width="250px;" />
            </td>
        </tr>
        <tr>
            <td align="right">Your Email id  :</td>
            <td align="left">
                <input id="txttoaddress" name="EmailId" width="250px" />
            </td>
        </tr>
        <tr>
            <td align="right">Phone No :</td>
            <td align="left">
                <input id="txtPhoneno" name="PhoneNo" width="250px" />
            </td>
        </tr>
        <tr>
            <td align="right">Subject :</td>
            <td align="left">
                <input id="txtsubject" name="Subject" width="250px"></input>
            </td>
        </tr>
        <tr>
            <td align="right">Message :</td>
            <td align="left">
                <textarea rows="4" cols="50" name="Message" id="txtmessage"></textarea>
            </td>
        </tr>
        <tr>
            <td align="right">Attachment :</td>
            <td align="left">
                <input type="file" name="file" />
            </td>
        </tr>

        <tr>
            <td align="center" colspan="2">
                <input type="submit" text="Send Message" />
            </td>
        </tr>
    </table>

This works but not the way I want.这有效,但不是我想要的方式。 By running this code, I insert the information and I receive it on the email inserted on the form, not the email in the code.通过运行此代码,我插入了信息,并在插入到表单的电子邮件中收到了它,而不是代码中的电子邮件。 I want the users to write their own email on the form, and by clicking the button I receive the information on my email.我希望用户在表单上写下他们自己的电子邮件,然后通过单击按钮我会收到我的电子邮件中的信息。 Can someone help me?有人能帮我吗?

A simple HTML page is not going to allow you to send email, even when setting the action to point to the page itself. 一个简单的 HTML 页面不会允许您发送电子邮件,即使将 action设置为指向页面本身也是如此。 You need to have some logic on the server to receive your form submission, process it, and send out the email. 您需要在服务器上设置一些逻辑来接收表单提交、处理它并发送电子邮件。

\n

Simply searching contact page in c# in a common search engine will provide dozens of tutorials and videos to walk you through the process. 只需在常用搜索引擎中搜索 contact page in c#中的 contact page in c#提供数十个教程和视频来引导您完成整个过程。 One example of How to create Contact Us Page in ASP.Net at ASPSnippets.com is a more simplified example. 在 ASPSnippets.com 的 ASP.Net如何创建联系我们页面的一个示例是一个更简化的示例。 There is a more complicated version at Codeproject.com shows How to Implement Contact Us Page in ASP.NET MVC (ASP.NET 5 ) Codeproject.com 上有一个更复杂的版本,展示了 如何在 ASP.NET MVC (ASP.NET 5) 中实现联系我们页面

Edit:编辑:

Looking at what you are asking, you want to figure out how to have the user send you a message from their email address.看看您要问什么,您想弄清楚如何让用户从他们的电子邮件地址向您发送消息。 It looks like the problem you have is a couple of parameters are flipped around.看起来您遇到的问题是几个参数被翻转了。

You have:你有:

...
mail.To.Add(EmailId);
mail.From = new MailAddress("xoinas23@gmail.com");
mail.Subject = Subject;
...

This will set the To for the email to the address the user entered.这将设置为电子邮件用户输入的地址。 This will direct the email to them and not the address to specified in code.这会将电子邮件定向到他们,而不是代码中指定的地址。

But you need:但你需要:

...
mail.To.Add("xoinas23@gmail.com");
mail.From = new MailAddress(EmailId);
mail.Subject = Subject;
...

This will set the email as being From the address the user entered and sending it To the address you specied in code.这会将电子邮件设置为来自用户输入的地址并将其发送您在代码中指定的地址。

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

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