错误编译时:"预期类,委托,枚举,接口或结构" [英] Errors when compiling: "Expected class, delegate, enum, interface, or struct"

查看:392
本文介绍了错误编译时:"预期类,委托,枚举,接口或结构"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段程式码有什么问题?这个程序是要复制一个文件,并通过电子邮件发送到一个电子邮件地址,但它不是。

What is wrong with this code? This program is meant to copy a file and email it to a email address, but it doesn't.

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);

}
}

这显示以下编译器错误:

This shows the following compiler errors:


  1. 预期的类,委托,枚举,介面或结构

  2. 预期的类别,委托,枚举,interface或struct

  3. 期望的类,委托,枚举,接口或结构
  4. / li>
  5. 预期类,委托,枚举,接口或结构

  6. 预期的类,委托,枚举,接口或结构

  7. 类型或命名空间定义或期望的文件结尾
    期望的类,委托,枚举,接口或结构

  1. Expected class, delegate, enum, interface, or struct
  2. Expected class, delegate, enum, interface, or struct
  3. Expected class, delegate, enum, interface, or struct
  4. Expected class, delegate, enum, interface, or struct
  5. Expected class, delegate, enum, interface, or struct
  6. Expected class, delegate, enum, interface, or struct
  7. Type or namespace definition, or end-of-file expected Expected class, delegate, enum, interface, or struct


$ b $

What can I do about this?

email_send()

解决方案

$ c>方法是在类声明之外。它仍然在命名空间内,但您还必须将其放在类中。此外,该方法从未被调用过。

The email_send() method is outside of the class declaration. It's still inside the namespace, but you must also place it inside the class. Additionally, at no point is the method ever called. It's dead code.

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

Move the method inside your class definition and then call the method from inside Form_Load()

这篇关于错误编译时:"预期类,委托,枚举,接口或结构"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆