与SmtpClient一起发送的MailMessage - BCC无法正常工作

时间:2016-03-30 15:23:49

标签: c# smtpclient mailmessage bcc

我尝试使用SMTP客户端发送带有BCC的电子邮件。我正在使用.net 4.5版。下面的代码成功地将电子邮件发送到收件人地址,但完全忽略了BCC地址,没有错误。

        var fromAddress = new MailAddress("from@email.com");
        var bccAddress = new MailAddress("bcc@email.com");

        var toAddress = new MailAddress("to@email.com");
        const string fromPassword = "mypass";

        var smtp = new SmtpClient
        {

            Host = "smtp.office365.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };

        var message = new MailMessage(fromAddress, toAddress);
        message.IsBodyHtml = true;
        message.Subject = email.EmailSubject;
        message.Body = email.EmailBody;
        message.Bcc.Add(bccAddress);

        smtp.Send(message);

我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

我注意到你的所有变量都被声明为" var"而不是他们的数据类型。当你明确指出会发生什么?

    MailAddress fromAddress = new MailAddress("from@email.com");
    MailAddress bccAddress = new MailAddress("bcc@email.com");

    MailAddress toAddress = new MailAddress("to@email.com");
    const string fromPassword = "mypass";

    SmtpClient smtp = new SmtpClient
    {

        Host = "smtp.office365.com",
        Port = 587,
        EnableSsl = true,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
    };

    MailMessage message = new MailMessage(fromAddress, toAddress);
    message.IsBodyHtml = true;
    message.Subject = email.EmailSubject;
    message.Body = email.EmailBody;
    message.Bcc.Add(bccAddress);

    try 
    {
        smtp.Send(message);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error sending: " + ex.ToString());
    }

您还可以在Bcc.Add之后设置断点,以检查消息对象以查看当前MailAddressCollection包含的内容。

答案 1 :(得分:0)

原来是邮箱问题,严重拖延了电子邮件的接收。