2013-05-28 50 views
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.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 [email protected]"); 
    mail.To.Add("[email protected]"); 
    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 [email protected]", "your password"); 
    SmtpServer.EnableSsl = true; 

    SmtpServer.Send(mail); 

} 
} 

这表明以下编译器错误:

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

我该怎么办?

回答

2

你方法是在课堂之外的一件事。将它复制到表格1类中,它应该清除任何智能问题

1

email_send方法未在类中定义。

15

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 [email protected]"); 
     mail.To.Add("[email protected]"); 
     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 [email protected]", "your password"); 
     SmtpServer.EnableSsl = true; 

     SmtpServer.Send(mail); 

    } 
} 
} 

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

相关问题