2017-01-13 2 views
-2

おはよう。私はどのように同じヘルパークラスの別の部分からヘルパークラス内の関数を呼び出すことができます把握しようとしているつもりですか?ヘルパークラスは、SMTP Sending Helperクラスを使用しようとしています。あまりにも多くのコーディングをする前に、これを実行できることを確認したいと思います。クラスライブラリ内でヘルパークラスを呼び出すWindowsサービス

ヘルパークラスAは自分のメール送信者ヘルパーです ヘルパークラスBは、電子メールの受信者を決定した後、電子メール警告を送信します。

ここまで私がこれまで何をしようとしています。

 ServiceLibrary.SmtpHelperClass smtp = new SmtpHelperClass(); 

私はSMTPを使用しようとすると:私は単にオブジェクトを作成するには、このようなヘルパークラスを呼び出すことができた印象の下にあったクラスB

内部クラスAのために使用して句を設定します。 SendMail(...);それは誤りです。これがどのように行われたかについて、誰かが何らかの光を当てることができるかこれらのヘルパークラスは、Windowsサービスで動作します。私の計画は、予定された実行時間に基づいて他のヘルパーに電話することです。

発信者コードがそうのように書かれている:

class AuditReminders 
{ 
    SmtpHelperClass mailHelper = new SmtpHelperClass(); 
    mailHelper.SendMailMessage(); 
} 

私はSendMailMessageは、このコンテキスト内に存在しないというエラーを取得します。私SmtpHelperClassはとてもように書かれている:

using System; 
using System.Net.Mail; 

namespace ServiceLibrary 
{ 
    public class SmtpHelperClass 
    { 
     public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body) 
     { 
      System.Diagnostics.EventLog evlFormatter = new System.Diagnostics.EventLog(); 
      evlFormatter.Source = "WAST Windows Service"; 
      evlFormatter.Log = "WAST Windows Service Log"; 

      // 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)) 
      { 
       string[] words = cc.Split(';'); 
       foreach (string word in words) 
        try 
        { 
         mMailMessage.CC.Add(new MailAddress(word)); 
        } 
        catch (Exception ex) 
        { 
         // place writer for event viewer here 
         evlFormatter.WriteEntry("Error encountered: " + ex.ToString()); 
        } 
       // Set the CC address of the mail message 

      } // 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.High; 

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

エラーは何ですか?クラス自体とは何の関係もないかもしれません。 – Tim

+0

私はこのようにテストのためにこれを書いた: クラスAuditReminders { SmtpHelperClass mailHelper = new SmtpHelperClass(); mailHelper.SendMailMessage(); } mailHelperは現在のコンテキストに存在しません。 – bbcompent1

+0

書きましたか?必要に応じて質問を編集して、より多くの情報とコードを追加することができます。 – Tim

答えて

1

あなたはMailHelperプロジェクトであるServiceLibraryアセンブリへの参照を持っていますか?あなたはそれを使用しようとしているプロジェクトにusing ServiceLibrary;というステートメントを持っていますか?それらはあなたが得ているエラーの最も一般的な理由です。

また、あなたのコードにはいくつかのことがあります。まず、SendMailMessagestaticとしてマークされていますが、それはインスタンスメソッドだようにそれを呼び出すようにしようとしている:mailHelper.SendMailMessage();

は第二に、SendMailMessageは、通話中に供給され、いずれもパラメータを、持っています。最初の問題については

、あなたはこのように(パラメータで)静的メソッドを呼び出すことができますから、BCC、へ、CC、件名と本文は、あなたがしたい値を持つ変数です

SmtpHelperClass.SendMailMessage(from, to, bcc, cc, subject, body); 

つかいます。

またはインスタンスメソッドであるための方法を変更する(staticキーワードを削除):

public void SendMailMessage((string from, string to, string bcc, string cc, string subject, string body)) 
+0

実際に私はそれを理解しました、それは私がそのプロセスを呼び出しようとしていた方法でした。また、いくつかの奇妙な理由から、メインプロジェクトはヘルパーライブラリへの参照を失った。すべて今良い、ありがとう:) – bbcompent1

0

根本的な原因は2倍でした。最初の問題は、そのライブラリへの参照が失われたため、クラスを正しく呼び出せなかったことです。一度私は、参照を固定し、その後、私は図書館に他のメソッドにアクセスするだけでなく、SmtpMailヘルパー関数を呼び出すことが許さ

using ServiceLibrary.SmtpHelperClass; 

を使用しました。

関連する問題