简体   繁体   English

C#MVC将电子邮件发送给多个收件人

[英]C# MVC Send email to multiple recipient

I have trying to send one mail to Test1 and Test2. 我试图将一封邮件发送到Test1和Test2。 I tried separating the recipients with ; 我试图用分隔收件人; like To ="Test1@stanleytests.co.za;Test2@stanleytests.co.za" that did not work and also tried concatenating them by doing To="Test1@stanleytests.co.za"+"Test2@stanleytests.co.za" that did not work. To ="Test1@stanleytests.co.za;Test2@stanleytests.co.za" ,并尝试通过执行To="Test1@stanleytests.co.za"+"Test2@stanleytests.co.za"串联它们To="Test1@stanleytests.co.za"+"Test2@stanleytests.co.za"无效。 now I wrote and Array. 现在我写了Array。 the thing with my array is that it sends 2 mails, so i only want to sent one mail to two recipient. 我的数组的问题是它发送2封邮件,所以我只想发送一封邮件给两个收件人。

private void SendDailyEmails(DateTime today)
 {
   today = DateTime.Now;
   string recipient = "Test1@stanleytests.co.za,Test2@stanleytests.co.za";
   string[] emailTo = recipient.Split(',');

    for (int i = 0; i < emailTo.GetLength(0); i++)
     {
       var emailObject = new EmailObject
        {
          To = emailTo[i],
          Cc = "me@stanleytests.co.za",
          Subject = "Daily Mail",
          Body = "Good morning, <br/><br/> This email is sent to you: <strong> "please be adviced" </strong> <br/><br/>Regards"
         };
          _emailService.SendEmail(emailObject);
     }  
 } 

Please assist here.thanks 请在这里帮助。谢谢

We don't know what library you are using for sending emails thus I can only make suggestions. 我们不知道您使用哪个库发送电子邮件,因此我只能提出建议。

The convention for joining several email address is to separate them with ; 加入多个电子邮件地址的惯例是将它们分开; :

emailObject.To = String.Join(";", recipient.Split(','));

Partially your code, see the example below. 部分代码,请参见下面的示例。 Honestly, I don't have access to our SMTP servers here, so, I can't really test it. 老实说,我在这里无权访问我们的SMTP服务器,因此,我无法真正对其进行测试。 This should set you on the right path. 这将使您走上正确的道路。 I am guessing your issue really is that you are missing: new MailAddress(i) . 我猜您的问题确实是您丢失了:new MailAddress(i)。

Hope this helps, there are more reference material on MSDN's site. 希望这会有所帮助,MSDN站点上有更多参考资料。

private void SendDailyEmails()
    {

        var today = DateTime.Now;
        var recipient = "Test1@stanleytests.co.za,Test2 @stanleytests.co.za";

        var message = new MailMessage()
        {
            From =  new MailAddress("Somebody"),
            CC = { new MailAddress("me@stanleytests.co.za") },
            Subject = "Daily Mail",
            Body = @"Good morning, <br/><br/> This email is sent to you: <strong> ""please be adviced"" </strong> <br/><br/>Regards",
            IsBodyHtml = true
        };

        foreach (var i in recipient.Split(',').ToList())
        {
            message.To.Add(new MailAddress(i));
        }

        // do your "_emailService.SendEmail(message);
    }
string body = "Body of email";
var message = new MailMessage();
message.To.Add(new MailAddress("example@exaple.com"));
message.To.Add(new MailAddress("example2@exaple.com"));
message.From = new MailAddress("example@gmail.com", "Name");  
message.Subject = "This is the subject";
message.Body = body;
message.IsBodyHtml = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM