非静态字段、方法或属性System.Net.Mail.SmtpClient.EnableSsl.get需要对象引用
本文关键字:EnableSsl SmtpClient Mail get 对象引用 Net System 字段 静态 方法 属性 | 更新日期: 2023-09-27 18:20:12
有人能帮我纠正这个错误吗?我基本上是在尝试创建一个个人电子邮件客户端,以更熟悉语言,但我似乎找不到如何解决这个问题。这是代码。
private void fusionButton1_Click(object sender, EventArgs e)
{
String One = textBox1.Text;
String Two = textBox2.Text;
String Three = textBox3.Text;
String Four = textBox4.Text;
MailMessage message = new MailMessage();
message.From = new MailAddress("Sender@gmail.com");
message.To.Add(new MailAddress(String.Copy(Four)));
message.Subject = String.Copy(Three);
message.Body = String.Copy(One); String.Copy(Two);
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.Send(message);
NetworkCredential netCre = new NetworkCredential("Sender@gmail.com", "Pass");
client.UseDefaultCredentials = false;
SmtpClient.EnableSsl = true;
使用此行:
client.EnableSsl = true;
而不是这个:
SmtpClient.EnableSsl = true;
您将非静态属性EnableSsl
引用为静态。
注意:
您不需要使用String.Copy
:
message.Subject = String.Copy(Three);
你可以直接使用这个:
message.Subject = textBox3.Text;
message.Body = textBox1.Text + " " + textBox2.Text;
试试这个代码:
private void fusionButton1_Click(object sender, EventArgs e)
{
MailMessage message = new MailMessage();
message.From = new MailAddress("Sender@gmail.com");
message.To.Add(new MailAddress(textBox4.Text));
message.Subject = textBox3.Text;
message.Body = textBox1.Text + " " + textBox2.Text;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("Sender@gmail.com", "Pass");
client.EnableSsl = true;
client.Send(message);
}