邮件不会转到抄送和密送

时间:2014-06-05 06:52:33

标签: asp.net-mvc c#-4.0

以下是我在c#中发送代码的邮件。邮件转到地址,但有时不会去抄送和密送。它终止了。

     MailMessage Msg = new MailMessage();
            MailAddress fromMail = new MailAddress(fromEmail);
            Msg.From = fromMail;
            foreach (var address in toEmail.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
            {
                Msg.To.Add(new MailAddress(address));
            }

            if (ccEmail != "" && bccEmail != "")
            {
                foreach (var address1 in ccEmail.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
                {
                    Msg.CC.Add(new MailAddress(address1));
                }

                foreach (var address2 in bccEmail.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
                {
                    Msg.Bcc.Add(new MailAddress(address2));
                }
            }
 SmtpClient a = new SmtpClient("smptserver");
            a.Send(Msg);

有人可以帮助我知道为什么不会去Cc和Bcc吗?

1 个答案:

答案 0 :(得分:0)

您正在检查CC BCC地址是否可用,然后再发送给其中任何一个。因此,如果两者都可用,您实际上只发送CC / BCC。仅发送到CC或仅发送到BCC将不适用于您的代码。这是为了吗?

如果您要发送到CC或BCC(取决于可用的内容),请单独检查它们是否为空

  if (!string.IsNullOrEmpty(ccEmail))
  {
      foreach (var address1 in ccEmail.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
     {
        Msg.CC.Add(new MailAddress(address1));
     }
  }

  if(!string.IsNullOrEmpty(bccEmail))
  {
      foreach (var address2 in bccEmail.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
      {
         Msg.Bcc.Add(new MailAddress(address2));
      }
  }