下面的代码是将邮件发送到一个电子邮件地址,但我想发送到多个电子邮件地址. [英] Below Code Is Send Mail To One Email Address, Bt I Want To Send To Many Email Addresses..How Do I Pass Multiple Mail Addresses Through To Field?
本文介绍了下面的代码是将邮件发送到一个电子邮件地址,但我想发送到多个电子邮件地址.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public ActionResult Index(MyMailModel objModelMail, string submit)
{
if (submit == "Productscar")
{
string from1 = "mandla.anilbabu@gmail.com";
string email1 = objModelMail.Email;
using (MailMessage mail = new MailMessage("mandla.anilbabu@gmail.com",from1))
{
mail.Subject = "This is subject new cars theam";
mail.Body = "New cars Theam 1 selected" + Environment.NewLine + "FROM :" + email1;
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential networkCredential = new NetworkCredential(from1, "PWD");
smtp.UseDefaultCredentials = true;
smtp.Credentials = networkCredential;
smtp.Port = 587;
smtp.Send(mail);
ViewBag.Message = "Sent";
ViewData["Message"] = "Saved theam successfully,this will update soon...";
return View("Index", objModelMail);
}
}
}
型号:
Model:
public class MyMailModel
{
public string From { get; set; }
[Required(ErrorMessage = "Please enter your email address")]
[RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Please enter correct email")]
public string Email { get; set; }
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
推荐答案
如您所见,属性System.Net.Mail.MailMessage.To
是一个集合.因此,如果集合中可以有一个元素,则可以有任意多个元素,每个元素代表不同的电子邮件地址:
https://msdn.microsoft.com/en-us/library/94z1w6d7%28v = vs.110%29.aspx [ ^ ],
https://msdn.microsoft.com/en -us/library/system.net.mail.mailaddresscollection(v = vs.110).aspx [
As you can see, the propertySystem.Net.Mail.MailMessage.To
is a collection. So, if you can have one element in the collection, you can have any number of them, each representing different e-mail address:
https://msdn.microsoft.com/en-us/library/94z1w6d7%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.net.mail.mailaddresscollection(v=vs.110).aspx[^].
—SA
轻松尝试一下..
easy try this..
foreach (var address in addresses.Split(new [] {";"}, StringSplitOptions.RemoveEmptyEntries))
{
mailMessage.To.Add(address);
}
这篇关于下面的代码是将邮件发送到一个电子邮件地址,但我想发送到多个电子邮件地址.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文