c# - 为什么我在发送 SMTP 电子邮件时收到“"' 属性无法分配”?

标签 c# email smtpclient

我不明白为什么这段代码不起作用。我收到一条错误消息,指出无法分配属性

MailMessage mail = new MailMessage();
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
mail.To = "user@hotmail.com"; // <-- this one
mail.From = "you@yourcompany.example";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);

最佳答案

mail.Tomail.From 是只读的。将它们移至构造函数。

using System.Net.Mail;

...

MailMessage mail = new MailMessage("you@yourcompany.example", "user@hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);

关于c# - 为什么我在发送 SMTP 电子邮件时收到“"' 属性无法分配”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33004590/

相关文章:

c# - 如何在调整大小的表单上安装控件

c# - 在文本框中输入字符的击键?

c# - 条件运算符的使用

email - 解释 VERP

email - 通过电子邮件将 Mercurial 存储库与 bundle 同步的工作流程

c# - 使用 Mailkit : "The SMTP server has unexpectedly disconnected."

android.os.NetworkOnMainThreadException 从 Android 发送电子邮件

c# - 将字节数组作为新程序执行

c# - 发送电子邮件时如何修复 'No connection could be made because the target machine actively refused it' 错误

c# - Asp.Net Identity 2.0 - 如何实现 IIdentityMessageService 以使用 SmtpClient 执行异步 SMTP?