2013-05-28 87 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方法現在位於類聲明中。

相關問題