Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errors when compiling: "Expected class, delegate, enum, interface, or struct"

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

}
}

This shows the following compiler errors:

  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

What can I do about this?

like image 685
Cookie Monster Avatar asked Nov 27 '22 21:11

Cookie Monster


1 Answers

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.

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

like image 80
Joel Coehoorn Avatar answered Jun 02 '23 02:06

Joel Coehoorn