我們試圖一次發送多封電子郵件給公司,其中電子郵件可以被選中或取消選中的列表。如果電子郵件被選中,則需要發送給他們。我設法讓它發送郵件到每個電子郵件地址打勾,但我們希望它是一次一個。 IE瀏覽器,而不是全部一次性發送一封電子郵件Contact1,Contact2,Contact3,我們要發送一封電子郵件Contact1,遍歷,然後第二次發送相同的電子郵件Contact2,等等。迭代通過複選框,發送電子郵件和循環
試過foreach循環,但它只是發送電子郵件三次給每個收件人,而不是一個收件人在同一時間,而只顯示在「收件人」字段的電子郵件。
發送電子郵件的代碼如下:
var smtp = new SmtpClient
{
Host = "",
Port = 25,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = true,
};
//Set message details, ensuring HTML is displayed when sent
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = true,
BodyEncoding = System.Text.Encoding.UTF8
})
//Try send the email to the users
try
{
smtp.Send(message); //Send the message to selected users
Response.Redirect("~/BulkEmail/Index"); //Redirect back to the Index page if send is successful.
}
catch
{
Response.Redirect("~/Home/Failure");
}
這工作得很好,但因爲它會去到外部的郵件,我們希望他們在每一次收到一個。郵件收集和「檢查」的視圖如下:
@{
string Email;
Email = User.Identity.Name.Split('\\')[1] + "@email.email";
}
@{
int count = 1;
}
@using (Html.BeginForm("SendEmail", "BulkEmail", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<label>To:</label> @User.Identity.Name.Split('\\')[1]<br />
<label>From:</label> <input type="text" name="txtFrom" value="@Email" /><br />
<table border="1" width="100%">
<label>BCC:</label>@foreach (var item in Model)
{
<tr width="100%">
<td>
@count
</td>
<td wdith="25">
@Html.DisplayFor(modelItem => item.FirstName) @Html.DisplayFor(modelItem => item.LastName)
</td>
<td wdith="25">
@Html.DisplayFor(modelItem => item.tblContact.Company)
</td>
<td wdith="25">
@Html.DisplayFor(modelItem => item.Email)
</td>
<td wdith="25" align="center">
<input type="checkbox" name="chkBox" value="@item.Email" checked="checked"/>
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.MailRecipientId }) |
@Html.ActionLink("Details", "Details", new { id = item.MailRecipientId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.MailRecipientId })
</td>
</tr>
count += 1;
}
</table><br />
<label>Subject:</label> <input type="text" name="txtSubject" value="Quotations Needed"/>
<textarea name="message"></textarea><br />
<input type="submit" value="BOOM THE EMAILS!" />
}
@Html.Action("TinyMCE", "Include")
任何幫助,將不勝感激。我覺得這應該是一個foreach循環,但我不是對如何正確地執行它在這種特定情況下確保100%。
感謝
請求的代碼:
var fromName = @User.Identity.Name.Split('\\')[1];
var fromAddress = Request.Form["txtFrom"];
var toAddress = Request.Form["chkBox"];
var subject = Request.Form["txtSubject"];
var q = Request.Unvalidated.Form;
var messageBody = q["message"];
控制器更新
public ActionResult SendEmail()
{
//Create variables for where the message is going internally, setting the subject and body whilst allowing an unvalidated HTML tinyMCE box to be posted.
var fromName = @User.Identity.Name.Split('\\')[1];
var fromAddress = Request.Form["txtFrom"];
var toAddress = Request.Form["chkBox[]"];
var subject = Request.Form["txtSubject"];
var q = Request.Unvalidated.Form;
var messageBody = q["message"];
String body = messageBody;
MailMessage mail_client = new MailMessage();
int index = 0;
foreach (var email in toAddress)
{
mail_client.To.Add(chkBox[index]);
index++;
}
mail_client.From = new MailAddress(fromAddress);
mail_client.Subject = subject;
mail_client.IsBodyHtml = true;
mail_client.Body = body;
{
//Create new SMTP client and give server details
var smtp = new SmtpClient
{
Host = "",
Port = 25,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = true,
};
//Set message details, ensuring HTML can be displayed when sent
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = true,
BodyEncoding = System.Text.Encoding.UTF8
})
//Try send the email to the users
try
{
smtp.Send(message); //Send the message to selected users
Response.Redirect("~/BulkEmail/Index"); //Redirect back to the Index page if send is successful.
}
//If the email doesn't send, allow the page to redirect rather than giving a vile error!
catch
{
Response.Redirect("~/Home/Failure");
}
}
return View();
}
顯示在您檢查檢查列表和地址添加到要 – Sachu
@Sachu我已經添加了代碼「的toAddress」區域爲主,後期的代碼。然後在原始文章的第二個代碼塊中使用 –