[英]how to send email using asp.net mvc3
我是 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>
您可以為此使用 Jquery。
您將必須使用 jquery 監聽復選框更改事件,並且在更改復選框值時,您可以對將發送郵件的操作進行 ajax 調用。
這是有關如何在 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);
}
在您使用的按鈕上,請使用以下代碼
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.