如何使用asp.net mvc3发送电子邮件

时间:2012-11-26 10:37:06

标签: asp.net .net email

mvc3的新手。 当我点击按钮选择人员时,请帮助我向选定的人发送电子邮件。 我也有表格显示姓名,电子邮件和复选框。当我点击复选框时,邮件应发送到该邮箱地址。

@model IEnumerable<ConferenceRoomProject.Models.Users>

@using (Html.BeginForm("InviteAttentees", "Booking", FormMethod.Get))
{    
   @Html.DropDownList("Departments", new SelectList(ViewBag.departments))
   <input type="submit" value="Filter"/>
}

<table>

<tr>

    <td>

        <a href="@Url.Action("MyMeetings", "Event")" title="Invite">
        <img src="../images/invite.png" width="40px" height="30px" alt="Edit"/>
        </a>

    </td>

</tr>

</table> 

<table id="tblInviteAttentees">

<caption>Invite Attentees</caption>

    <tr>

        <th>

            Name

        </th>  

        <th>

            Email

        </th>  

        <th></th>
    </tr>

@foreach (var item in Model) {

    <tr>

        <td>

            @Html.DisplayFor(modelItem => item.Name)

        </td>  

        <td>

            @Html.DisplayFor(modelItem => item.Email)

        </td> 

        <td>

            <input type="checkbox" name="chkEmployee" id="chkEmployee"/>

        </td>       

    </tr>

}

</table>

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

你可以使用Jquery。

您必须使用jquery收听复选框更改事件,并且在更改复选框值时,您可make an ajax call指向将发送邮件的操作。

以下是有关如何在ASP.NET MVC 3中发送邮件的示例代码

public void SendEmail(string address, string subject, string message)
{
    string email = "yrshaikh.mail@gmail.com";
    string password = "put-your-GMAIL-password-here";

    var loginInfo = new NetworkCredential(email, password);
    var msg = new MailMessage();
    var smtpClient = new SmtpClient("smtp.gmail.com", 587);

    msg.From = new MailAddress(email);
    msg.To.Add(new MailAddress(address));
    msg.Subject = subject;
    msg.Body = message;
    msg.IsBodyHtml = true;

    smtpClient.EnableSsl = true;
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = loginInfo;
    smtpClient.Send(msg);
}

答案 2 :(得分:0)

在您使用的按钮上,请使用以下代码

button1_onClick()
{
string email = "YOUR G-Mail ID";
string password = "PASSWORD";

var loginInfo = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", 587);

msg.From = new MailAddress(email);
msg.To.Add(new MailAddress(address));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}