【问题标题】:Errors when compiling: "Expected class, delegate, enum, interface, or struct"编译时出错:“预期的类、委托、枚举、接口或结构”
【发布时间】:2013-05-23 21:37:51
【问题描述】:

这段代码有什么问题?该程序旨在复制文件并将其通过电子邮件发送到电子邮件地址,但事实并非如此。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }

    public void email_send()
   {
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress("your mail@gmail.com");
    mail.To.Add("to_mail@gmail.com");
    mail.Subject = "Test Mail - 1";
    mail.Body = "mail with attachment";

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
    mail.Attachments.Add(attachment);

    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
    SmtpServer.EnableSsl = true;

    SmtpServer.Send(mail);

}
}

这显示了以下编译器错误:

  1. 预期的类、委托、枚举、接口或结构
  2. 预期的类、委托、枚举、接口或结构
  3. 预期的类、委托、枚举、接口或结构
  4. 预期的类、委托、枚举、接口或结构
  5. 预期的类、委托、枚举、接口或结构
  6. 预期的类、委托、枚举、接口或结构
  7. 类型或命名空间定义,或预期的文件结尾 预期的类、委托、枚举、接口或结构

对此我能做些什么?

【问题讨论】:

    标签: c# compiler-errors


    【解决方案1】:

    你的方法是在类之外的一件事。将其复制到表单 1 类中,它应该可以清除所有智能感知问题

    【讨论】:

      【解决方案2】:

      email_send 方法未在类中定义。

      【讨论】:

        【解决方案3】:

        email_send() 方法在类声明之外。它仍在命名空间内,但您还必须将它放在类中。此外,从未调用过该方法。这是死代码。

        在类定义中移动方法,然后从 Form_Load() 中调用方法

        【讨论】:

          【解决方案4】:

          正是其他人所说的,但剪切/粘贴此内容,您应该更正错误:

          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Linq;
          using System.Text;
          using System.Windows.
          using System.Net;
          using System.Net.Mail;
          
          namespace WindowsFormsApplication1
          {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }
          
              private void Form1_Load(object sender, EventArgs e)
              {
          
              }
          
              private void button1_Click(object sender, EventArgs e)
              {
          
              }
          
              public void email_send()
              {
                  MailMessage mail = new MailMessage();
                  SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                  mail.From = new MailAddress("your mail@gmail.com");
                  mail.To.Add("to_mail@gmail.com");
                  mail.Subject = "Test Mail - 1";
                  mail.Body = "mail with attachment";
          
                  System.Net.Mail.Attachment attachment;
                  attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
                  mail.Attachments.Add(attachment);
          
                  SmtpServer.Port = 587;
                  SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
                  SmtpServer.EnableSsl = true;
          
                  SmtpServer.Send(mail);
          
              }
          }
          }
          

          如您所见,您的 email_send 方法现在位于类声明中。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-01-13
            • 1970-01-01
            • 2016-04-09
            • 1970-01-01
            相关资源
            最近更新 更多