【问题标题】:Property or indexer "Attachments" cannot be assigned to -- it is read only无法将属性或索引器“附件”分配给 - 它是只读的
【发布时间】:2016-07-27 01:58:45
【问题描述】:

我正在尝试发送带有图片附件的邮件,但仍然报错 (*不能将属性或索引器“附件”分配给——它是只读的 *)

string pathToPic = @"c:\MyDir\Img\img"+ automaticalyGeneratedNumber.toString() + ".png";


using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = Environment.MachineName,
                Body = "PC NAME : " + Environment.MachineName + "\r\nIP ADRESS : " + Dns.GetHostEntry(Dns.GetHostName()).AddressList[1],
                Attachments = new Attachment(@"c:\MyDir\Img" + "/img" + (Saving.CountImagesTaken(@"c:\MyDir\Img") - 1).ToString() + ".png"),
            })
            {
                smtp.Send(message);
            }

【问题讨论】:

    标签: c# email attachment


    【解决方案1】:

    为什么要使代码如此复杂。您需要使用message.Attachments.Add,因为Attachments 属性是只读的:

    var message = new MailMessage(fromAddress, toAddress)
    {
        Subject = Environment.MachineName,
        Body = "PC NAME : " + Environment.MachineName + "\r\nIP ADRESS : " + Dns.GetHostEntry(Dns.GetHostName()).AddressList[1],
    };
    message.Attachments.Add(new Attachment(@"c:\MyDir\Img" + "/img" + (Saving.CountImagesTaken(@"c:\MyDir\Img") - 1).ToString() + ".png"));
    using (message)
    {
        smtp.Send(message);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多