[英]sending mail to multiple recipients using asp.net
I have the following code inside my asp.net mvc-5 web application:- 我的asp.net mvc-5网络应用程序中包含以下代码:-
string from = "info@Oursystem.com";
using (MailMessage mail = new MailMessage(from, "user1@Oursystem.com"))
{
currently when i try to add multiple recipients for the email as follow :- 目前,当我尝试为电子邮件添加多个收件人时,如下所示:-
string from = "info@Oursystem.com";
using (MailMessage mail = new MailMessage(from, "user1@Oursystem.com;user2@Oursystem.com"))
{
it will not send any emails, so can anyone advice on this please ? 它不会发送任何电子邮件,所以任何人都可以对此提出建议吗?
Try ewith following namespaces. 尝试使用ewith以下命名空间。
string from = test@test.com;
string to = test123@testing.com;
string to1 =test1234@testing.com;
mail.From = new System.Net.Mail.MailAddress(from);
mail.To.Add(new System.Net.Mail.MailAddress(to));
mail.To.Add(new System.Net.Mail.MailAddress(to1));
MailMessage take a single reciver for to MailMessage只需接收一个收件人即可
public MailMessage( MailAddress from, MailAddress to )
公共MailMessage(MailAddress从,MailAddress到)
So you have to add objects for others recivers in to list 因此,您必须为其他收信人添加对象到列表中
Try this - 尝试这个 -
string from = "info@Oursystem.com";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(from);
mail.To.Add("user1@Oursystem.com");
mail.To.Add("user2@Oursystem.com");
}
The new MailMessage()
constructor takes arguments for both from
and to
, and should be used when you have just single recipient. new MailMessage()
构造函数接受from
和to
参数,并且当您只有单个收件人时应使用。 In case of multiple recipient, you have to add them to the mail.To
collection 如果有多个收件人,则必须将它们添加到
mail.To
public class MailHandler
{
public bool SendEMail(string smtpHost ="smtp.gmail.com", int port = 587, string senderMail , string senderPass, ArrayList mailToArr, string subject, bool isHtml, string body)
{
try
{
SmtpClient smtpClient = new SmtpClient(smtpHost, port);
smtpClient.UseDefaultCredentials = false;// true;
smtpClient.Credentials = new System.Net.NetworkCredential(senderMail, senderPass);
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
mail.From = new MailAddress(senderMail);
for (int i = 0; i < mailToArr.Count; i++)
{
mail.To.Add(new MailAddress((string)mailToArr[i]));
}
mail.Subject = subject;
//mail.CC.Add(new MailAddress("MyEmailID@gmail.com"));
mail.Body = body;
mail.IsBodyHtml = isHtml;
mail.Priority = MailPriority.Normal;
smtpClient.Send(mail);
return true;
}
catch (Exception ex)
{
return false;
// write exception on server log
}
}
}
//to call function //调用函数
ArrayList mailToArr = new ArrayList();
mailToArr.Add("to first email address");
mailToArr.Add("to second email address");
mailToArr.Add("to third email address");
MailHandler objMailhandler = new MailHandler();
string subject = "subject";
string body = "<h1> Password Request </h1> " ;
objMailhandler.SendEMail("smtp.gmail.com", 587, senderMail , senderPass,mailToArr, subject, true, body);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.