0

我有一个想在我的 Web 表单应用程序上使用的类文件。这是我在网上找到的一个。我已将它放在 App_Code 文件夹中,并尝试在活动中使用它

protected void sendBtn_Click(object sender, EventArgs e)
        {

        }

类文件:

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

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

SendEmail send = new SendEmail();

var send = new SendEmail();

SendEmail.SendMailMessage

using SendEmail

这一切都无济于事。

我是一个绝对的初学者

4

5 回答 5

1

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

这会让你——

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

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

然后你可以简单地在你的页面中使用以下内容-

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");
}

我怀疑你的课程没有编译是什么让你绊倒了。上面的代码都是在示例项目中构建的,没有问题。

尽管这超出了问题的范围,但我很想将帮助程序类设为静态,这样您就不必一直实例化 SendEmail 的新副本。

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

于 2013-03-17T17:50:10.193 回答
1

我想推荐这些:

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

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

  3. 将 SendMail 类设为静态类,将 SendMailMessage 设为静态函数。

于 2013-05-14T15:41:46.947 回答
0

将您的类文件更改为:

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

然后在你的 web 表单代码后面,添加:

using MyCompany;

以下是有关使用命名空间的更多信息,您可能会发现这些信息对您有所帮助。

于 2013-03-01T20:23:36.643 回答
0

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

于 2013-03-01T17:14:43.120 回答
0

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

您的新 SendMail 类将进入其中。

有时您似乎只需要一些帮助类,但数量会随着时间的推移而增长。随着项目的发展,将事物分开也有助于编译速度/组织。

于 2013-03-01T20:10:25.410 回答