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

他の作業を行う前に、SendEmail クラス ファイルに System.Net.Mail 名前空間を追加する必要があります。

これにより、あなたは-

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.aspxおよびhttp://msdn.microsoft.com/en-us/library/sf0df423.aspxを参照してください。

于 2013-03-17T17:50:10.193 に答える
0

通常、私が行うことは、ビジュアルスタジオソリューションで別の「ライブラリ」プロジェクトを作成し、それをメインのWebサイトプロジェクトから参照することです。

新しいSendMailクラスがその中に入れられます。

いくつかのヘルパークラスが必要なように見えることもありますが、その量は時間の経過とともに増加する傾向があります。物事を分離することは、プロジェクトの成長に伴うコンパイル速度/編成にも役立ちます。

于 2013-03-01T20:10:25.410 に答える
0

クラスファイルを次のように変更します。

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

次に、Web フォームのコード ビハインドに以下を追加します。

using MyCompany;

名前空間の使用に関する役立つ情報を次に示します。

于 2013-03-01T20:23:36.643 に答える