webmail.send无法正常工作

时间:2018-07-24 07:26:04

标签: html asp.net-mvc entity-framework webmail

我正在使用Web应用程序,当前正在创建联系页面。我在视图中创建了一个邮件表单,该表单由de controller通过HttpPOST处理。无论如何,由于某种原因,webmail.send无法正常工作。有人看到我在做什么错吗?

控制器仅包含smtp设置和发送邮件的代码。 这是控制器代码:

[HttpPost]
    public ActionResult Contact(string firstname, string lastname, string email, string subject, string message)
    {
        try
        {
            //smtp settings
            WebMail.SmtpServer = "smtp.google.com";
            WebMail.SmtpPort = 587;
            WebMail.SmtpUseDefaultCredentials = true;
            WebMail.EnableSsl = true;
            //smtp username
            WebMail.UserName = "email@gmail.com";
            WebMail.Password = "password";
            //receiver
            string EmailTo = "email@gmail.com";

            //send mail
            WebMail.Send(to: EmailTo, subject: subject, body: message, from: email);
            return Content("mail send");
        }
        catch (Exception ex)
        {
            string errorMessage = ex.Message;
            return Content(errorMessage);
        }
    }

该视图包含一些联系信息和表格。表单的内容作为参数提交给控制器。 和视图:

<div id="contactWrapper">
<div class="pageTitle">
    <p><span>Contact</span>Do you have questions or remarks? Feel free to contact us.</p>
</div>

<div id="contact">
    <div id="contactImage">
    </div>

    <div id="contactInfo">
        <h1>Pencil Art</h1>
        <div class="divider"></div>
        <p>
            <span class="wmt">W:</span>www.pencilart.com <br>
            <span class="wmt">M:</span>info@pencilart.nl <br>
            <span class="wmt">T:</span>+(31)6 105 22 848
        </p>
        <div class="divider"></div>
        <p>
            Bloemdwarsstraat 1 c <br>
            1016 LL Amsterdam <br>
            Nederland
        </p>
        <div class="divider"></div>
        <p>
            Kvk-no: 3216 544 871 234
        </p>
    </div>

    <div id="contactForm">
        <form method="POST">
            <div id="inputBox" class="singlelineBox">
                <input type="text" name="firstname" placeholder="First Name" class="singleTextLineInput">
            </div>

            <div id="inputBox" class="singlelineBox">
                <input type="text" name="lastname" placeholder="Last Name" class="singleTextLineInput">
            </div>

            <div id="inputBox" class="singlelineBox">
                <input type="email" name="email" placeholder="Email-Adress" class="singleTextLineInput">
            </div>

            <div id="inputBox" class="singlelineBox">
                <input type="text" name="subject" placeholder="Subject" class="singleTextLineInput">
            </div>

            <div id="inputBox" class="multlinebox">
                <textarea name="message" placeholder="Message"></textarea>
            </div>

            <input type="submit" value="Sent">
        </form>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

发件人地址必须与用于配置SMTP的帐户的地址相同。