1

I found this code I want to run in a script, but I have this error:

Script compilation error: Cannot declare namespace in script code

I want to finish a function that to send a email by C#

using System;
using System.Windows.Forms;
using System.Net.Mail;

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

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.sina.com");

                mail.From = new MailAddress("[email protected]");
                mail.To.Add("[email protected]");
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from SINA";

                SmtpServer.Port = 25;
                SmtpServer.Credentials = new System.Net.NetworkCredential("hello", "world");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("mail Send");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

How can I do this?

5
  • 3
    That's an unusual error message, what are you trying to compile it with? Commented Jun 7, 2018 at 7:14
  • 1
    Also, what do you mean by "when I run the query"? You've shown a Windows Forms application... I don't see any sign of a C# script or a query. Please give us more context - it may well be that you're trying to use C# in a context where this kind of code is inappropriate. Commented Jun 7, 2018 at 7:19
  • 1
    Are you trying to run this with csi? rosyln? Commented Jun 7, 2018 at 7:21
  • 1
    That code compiles and runs just fine on Visual Studio 17 as a C# Windows Forms project. Commented Jun 7, 2018 at 8:06
  • It seems you run scriptcs yourself. Please consider developing in a real IDE instead of using Notepad and compiling yourself. An IDE has many advantages... Commented Jun 7, 2018 at 11:32

1 Answer 1

2

Thanks everyone for your helping on this blog. I just have figured out on my own, just use the pure functional code, works well.

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.sina.com");

mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from SINA";

SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("hello", "world");
SmtpServer.EnableSsl = true;

SmtpServer.Send(mail);
Sign up to request clarification or add additional context in comments.

1 Comment

I ran into the same problem today. It took hours, but I finally got it to work. Did you ever happen to figure out why it doesn't work with the class & namespace?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.