如何在ASP.NET Web窗体应用程序中使用类文件?

时间:2021-08-24 15:16:27

I have a class file I'd like to use on my Web Forms Application. This is one that I found in the Internet. I have placed it in the App_Code folder and am trying to use it on the event

我有一个我想在我的Web窗体应用程序上使用的类文件。这是我在互联网上找到的。我已将它放在App_Code文件夹中,并尝试在事件中使用它

protected void sendBtn_Click(object sender, EventArgs e)
        {

        }

Class File:

班级档案:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class SendEmail
{
    public void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
    {
        // Instantiate a new instance of MailMessage
        MailMessage mMailMessage = new MailMessage();

        // Set the sender address of the mail message
        mMailMessage.From = new MailAddress(from);
        // Set the recepient address of the mail message
        mMailMessage.To.Add(new MailAddress(to));

        // Check if the bcc value is null or an empty string
        if ((bcc != null) && (bcc != string.Empty))
        {
            // Set the Bcc address of the mail message
            mMailMessage.Bcc.Add(new MailAddress(bcc));
        }      // Check if the cc value is null or an empty value
        if ((cc != null) && (cc != string.Empty))
        {
            // Set the CC address of the mail message
            mMailMessage.CC.Add(new MailAddress(cc));
        }       // Set the subject of the mail message
        mMailMessage.Subject = subject;
        // Set the body of the mail message
        mMailMessage.Body = body;

        // Set the format of the mail message body as HTML
        mMailMessage.IsBodyHtml = true;
        // Set the priority of the mail message to normal
        mMailMessage.Priority = MailPriority.Normal;

        // Instantiate a new instance of SmtpClient
        SmtpClient mSmtpClient = new SmtpClient();
        // Send the mail message
        mSmtpClient.Send(mMailMessage);
    }
}

I can't access the methods and have tried the following :

我无法访问这些方法,并尝试了以下方法:

SendEmail send = new SendEmail();

var send = new SendEmail();

SendEmail.SendMailMessage

using SendEmail

All of which to no avail.

所有这些都无济于事。

I am an absolute beginner

我是一个绝对的初学者

5 个解决方案

#1


1  

Before you do anything else you will need to add the System.Net.Mail namespace to your SendEmail class file.

在执行任何其他操作之前,您需要将System.Net.Mail命名空间添加到SendEmail类文件中。

This will leave you with-

这会让你 -

using System.Net.Mail;

public class SendEmail
{
    public void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
    {
        // Instantiate a new instance of MailMessage
        MailMessage mMailMessage = new MailMessage();

        // Set the sender address of the mail message
        mMailMessage.From = new MailAddress(from);
        // Set the recepient address of the mail message
        mMailMessage.To.Add(new MailAddress(to));

        // Check if the bcc value is null or an empty string
        if ((bcc != null) && (bcc != string.Empty))
        {
            // Set the Bcc address of the mail message
            mMailMessage.Bcc.Add(new MailAddress(bcc));
        }      // Check if the cc value is null or an empty value
        if ((cc != null) && (cc != string.Empty))
        {
            // Set the CC address of the mail message
            mMailMessage.CC.Add(new MailAddress(cc));
        }       // Set the subject of the mail message
        mMailMessage.Subject = subject;
        // Set the body of the mail message
        mMailMessage.Body = body;

        // Set the format of the mail message body as HTML
        mMailMessage.IsBodyHtml = true;
        // Set the priority of the mail message to normal
        mMailMessage.Priority = MailPriority.Normal;

        // Instantiate a new instance of SmtpClient
        SmtpClient mSmtpClient = new SmtpClient();
        // Send the mail message
        mSmtpClient.Send(mMailMessage);
    }
}

None of the other "usings" in there are required, these are added by default by Visual Studio but aren't actually used in the class.

其中没有其他“使用”是必需的,默认情况下这些是由Visual Studio添加的,但实际上并未在类中使用。

Then you can simply use the following in your page-

然后你可以在页面中使用以下内容 -

protected void sendBtn_Click(object sender, EventArgs e)
{
    SendEmail send = new SendEmail();

    send.SendMailMessage("from@domain.com", "to@domain.com", null, null, "Test e-mail", "Test e-mail");
}

I suspect the fact your class wasn't compiling was what was tripping you up. The above code all built without problems in a sample project.

我怀疑你的班级没有编译的事实是绊倒了你。上面的代码都在示例项目中没有问题地构建。

Although it is beyond the scope of the question I'd be tempted to make the helper classes static so you don't have to instantiate a new copy of SendEmail all the time.

虽然它超出了问题的范围,但我很想让帮助程序类保持静态,因此您不必一直实例化SendEmail的新副本。

See http://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx and http://msdn.microsoft.com/en-us/library/sf0df423.aspx for more details on the using directive.

有关using指令的更多详细信息,请参阅http://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx和http://msdn.microsoft.com/en-us/library/sf0df423.aspx。

#2


1  

I'd like to recommend these:

我想推荐这些:

  1. Add the namespace to your class file. using System.Net.Mail;

    将命名空间添加到类文件中。使用System.Net.Mail;

  2. Provide arguments when you call the function (SendMailMessage).

    调用函数(SendMailMessage)时提供参数。

  3. Make class SendMail as a static class, SendMailMessage as static function.

    将类SendMail作为静态类,将SendMailMessage作为静态函数。

#3


0  

Put your class file in the same folder where your code that calls sendBtn_Click is located. If your code with the handler has a line somewhere at the top that says namespace SomeNamespace {, make sure that the class file you copied also has the same line. Your curly braces may vary.

将您的类文件放在调用sendBtn_Click的代码所在的文件夹中。如果带有处理程序的代码在顶部某处有一行说明名称空间SomeNamespace {,请确保您复制的类文件也具有相同的行。你的花括号可能会有所不同。

#4


0  

Generally what I would do is create a separate "Library" project in your visual studio solution and reference that from your main website project.

一般来说,我要做的是在Visual Studio解决方案中创建一个单独的“库”项目,并从您的主网站项目中引用该项目。

Your new SendMail class will go in it.

您的新SendMail类将进入其中。

Sometimes it seems like you'll just need a few helper classes but the amount tends to grow over time. Separating things out also helps with compile speed/organization as the project grows.

有时似乎你只需要一些帮助类,但数量往往随着时间的推移而增长。随着项目的发展,分离出来也有助于编译速度/组织。

#5


0  

Change your class file to:

将您的类文件更改为:

namespace MyCompany
{
     public class SendEmail
     {
          //Class properties and methods here.
     }
}

Then in your web form code behind, add:

然后在您的Web表单代码后面添加:

using MyCompany;

Here is some more information about using namespaces which you might find helpful.

以下是有关使用命名空间的更多信息,您可能会发现它们很有帮助。

#1


1  

Before you do anything else you will need to add the System.Net.Mail namespace to your SendEmail class file.

在执行任何其他操作之前,您需要将System.Net.Mail命名空间添加到SendEmail类文件中。

This will leave you with-

这会让你 -

using System.Net.Mail;

public class SendEmail
{
    public void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
    {
        // Instantiate a new instance of MailMessage
        MailMessage mMailMessage = new MailMessage();

        // Set the sender address of the mail message
        mMailMessage.From = new MailAddress(from);
        // Set the recepient address of the mail message
        mMailMessage.To.Add(new MailAddress(to));

        // Check if the bcc value is null or an empty string
        if ((bcc != null) && (bcc != string.Empty))
        {
            // Set the Bcc address of the mail message
            mMailMessage.Bcc.Add(new MailAddress(bcc));
        }      // Check if the cc value is null or an empty value
        if ((cc != null) && (cc != string.Empty))
        {
            // Set the CC address of the mail message
            mMailMessage.CC.Add(new MailAddress(cc));
        }       // Set the subject of the mail message
        mMailMessage.Subject = subject;
        // Set the body of the mail message
        mMailMessage.Body = body;

        // Set the format of the mail message body as HTML
        mMailMessage.IsBodyHtml = true;
        // Set the priority of the mail message to normal
        mMailMessage.Priority = MailPriority.Normal;

        // Instantiate a new instance of SmtpClient
        SmtpClient mSmtpClient = new SmtpClient();
        // Send the mail message
        mSmtpClient.Send(mMailMessage);
    }
}

None of the other "usings" in there are required, these are added by default by Visual Studio but aren't actually used in the class.

其中没有其他“使用”是必需的,默认情况下这些是由Visual Studio添加的,但实际上并未在类中使用。

Then you can simply use the following in your page-

然后你可以在页面中使用以下内容 -

protected void sendBtn_Click(object sender, EventArgs e)
{
    SendEmail send = new SendEmail();

    send.SendMailMessage("from@domain.com", "to@domain.com", null, null, "Test e-mail", "Test e-mail");
}

I suspect the fact your class wasn't compiling was what was tripping you up. The above code all built without problems in a sample project.

我怀疑你的班级没有编译的事实是绊倒了你。上面的代码都在示例项目中没有问题地构建。

Although it is beyond the scope of the question I'd be tempted to make the helper classes static so you don't have to instantiate a new copy of SendEmail all the time.

虽然它超出了问题的范围,但我很想让帮助程序类保持静态,因此您不必一直实例化SendEmail的新副本。

See http://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx and http://msdn.microsoft.com/en-us/library/sf0df423.aspx for more details on the using directive.

有关using指令的更多详细信息,请参阅http://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx和http://msdn.microsoft.com/en-us/library/sf0df423.aspx。

#2


1  

I'd like to recommend these:

我想推荐这些:

  1. Add the namespace to your class file. using System.Net.Mail;

    将命名空间添加到类文件中。使用System.Net.Mail;

  2. Provide arguments when you call the function (SendMailMessage).

    调用函数(SendMailMessage)时提供参数。

  3. Make class SendMail as a static class, SendMailMessage as static function.

    将类SendMail作为静态类,将SendMailMessage作为静态函数。

#3


0  

Put your class file in the same folder where your code that calls sendBtn_Click is located. If your code with the handler has a line somewhere at the top that says namespace SomeNamespace {, make sure that the class file you copied also has the same line. Your curly braces may vary.

将您的类文件放在调用sendBtn_Click的代码所在的文件夹中。如果带有处理程序的代码在顶部某处有一行说明名称空间SomeNamespace {,请确保您复制的类文件也具有相同的行。你的花括号可能会有所不同。

#4


0  

Generally what I would do is create a separate "Library" project in your visual studio solution and reference that from your main website project.

一般来说,我要做的是在Visual Studio解决方案中创建一个单独的“库”项目,并从您的主网站项目中引用该项目。

Your new SendMail class will go in it.

您的新SendMail类将进入其中。

Sometimes it seems like you'll just need a few helper classes but the amount tends to grow over time. Separating things out also helps with compile speed/organization as the project grows.

有时似乎你只需要一些帮助类,但数量往往随着时间的推移而增长。随着项目的发展,分离出来也有助于编译速度/组织。

#5


0  

Change your class file to:

将您的类文件更改为:

namespace MyCompany
{
     public class SendEmail
     {
          //Class properties and methods here.
     }
}

Then in your web form code behind, add:

然后在您的Web表单代码后面添加:

using MyCompany;

Here is some more information about using namespaces which you might find helpful.

以下是有关使用命名空间的更多信息,您可能会发现它们很有帮助。