如何在Asp.Net Mvc中将电子邮件帐户与联系页面同步并在该电子邮件帐户上接收消息

时间:2017-04-28 09:41:46

标签: c# asp.net-mvc email

我的Asp.Net MVC应用程序中有一个联系页面,任何访问者都可以留下他们的消息。我希望在我的电子邮件帐户中收到此消息。例如,我想以这样的方式配置此应用,即可以将留言发送到我的 gmail 或任何其他帐户。我想知道.NET C#中是否有任何方法可以帮助我完成任务。

谢谢。

2 个答案:

答案 0 :(得分:0)

请阅读发布指南并首先查看文档。您应该尝试该问题,然后发布不起作用的代码。

鉴于这是你的第一篇帖子......

public static void CreateTestMessage2(string server)
{
    string to = "jane@contoso.com";
    string from = "ben@contoso.com";
    MailMessage message = new MailMessage(from, to);
    message.Subject = "Using the new SMTP client.";
    message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
    SmtpClient client = new SmtpClient(server);
    // Credentials are necessary if the server requires the client 
    // to authenticate before it will send e-mail on the client's behalf.
    client.UseDefaultCredentials = true;

    try {
      client.Send(message);
    }  
    catch (Exception ex) {
      Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", 
                  ex.ToString() );            
    }              
}

https://msdn.microsoft.com/en-us/library/swas0fwc(v=vs.110).aspx

答案 1 :(得分:0)

希望此示例适合您:

创建模型类:

public class SampleMail
    {
        public string Name { get; set; }

        public string Message { get; set; }
    }

为此创建一个动作:

       [HttpGet]
        public ActionResult ContactMail()
        {


            return View();
        }

        [HttpPost]
        public ActionResult ContactMail(SampleMail sm)
        {
            SmtpClient MailClient = new SmtpClient();
        MailClient.Host = "Server Name";// smtp server name which you are going to use
        MailClient.Port = 25; // port number
        MailClient.Credentials=new System.Net.NetworkCredential("yourusername", "yourpassword");
        MailMessage msg = new MailMessage();
        msg.Body = sm.Message + "</br> By" + sm.Name;
        msg.To.Add(new MailAddress("xyz@gmail.com ")); // change the mail with your email address
        msg.IsBodyHtml = true;

        MailClient.Send(msg);


            return View();
        }

创建联系人邮件视图:

 @model sampleContactMail.Models.SampleMail

@{
    ViewBag.Title = "ContactMail";
}

<h2>ContactMail</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>SampleMail</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Send" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}