-2

我是 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>
4

2 回答 2

0

You can use Jquery for this.

You will have to listen to the checkbox change event using jquery and on change of the checkbox value you can make an ajax call to your action which will send the mail.

Here is a sample code on how to send mail in 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);
}
于 2012-11-26T10:44:29.963 回答
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);
}
于 2012-11-26T10:52:59.820 回答