我们一次尝试将多封电子邮件发送到公司列表,可以检查或取消选中电子邮件。如果选中了电子邮件,则需要将其发送给他们。我设法让它将电子邮件发送到每个已勾选的电子邮件地址,但我们希望它一次只能一个。 IE,而不是一次性向Contact1,Contact2,Contact3发送一封电子邮件,我们希望向Contact1发送一封电子邮件,循环播放,然后再次向Contact2发送相同的电子邮件,依此类推。
尝试了一个foreach循环,但它只是向每个收件人发送三次电子邮件,而不是一次向一个收件人发送电子邮件,同时仅在" To"中显示他们的电子邮件。字段。
发送电子邮件的代码如下:
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();
}
答案 0 :(得分:1)
将您要发送的所有电子邮件地址添加到阵列中,例如:
//Email Address
List<string> recipients= new List<string>();
recipients.Add("Email1");
recipients.Add("Email2");
recipients.Add("Email3");
recipients.Add("Email4");
recipients.Add("Email5");
然后像这样做一个for循环:
for (int i = 0; i < recipients.Count; i++)
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.IsBodyHtml = true;
message.To.Add(recipients[i]);
message.Subject = subject;
message.From = from;
message.Body = body;
System.Net.Mail.SmtpClient smtp = smtpClient;
smtp.Send(message);
}
答案 1 :(得分:1)
将chkbox的名称更改为“chkbox []” 因此,在控制器中,您将获得一个参数(string [] chkbox),其中只包含已检查的参数..
然后创建一个邮件消息对象
classifier
然后您可以使用相同的方式发送邮件