Windows class 库中的服务调用助手 class

Windows Service calling helper class inside class library

早上好。我一直在试图弄清楚如何从同一助手 class 的另一部分调用助手 class 内的函数?我的助手 class 正在尝试使用 SMTP 发送助手 class。在我深入编码之前,我想确保这可以完成。

Helper class A 是我的邮件发件人助手 助手 Class B 在确定谁应该接收电子邮件后发送电子邮件警报。

这就是我到目前为止尝试做的事情。我在 class B 中为 class A 设置了一个 using 子句。

我的印象是我可以像这样简单地调用助手 class 来创建一个对象:

        ServiceLibrary.SmtpHelperClass smtp = new SmtpHelperClass();

当我尝试使用 smtp.SendMail(...);它出错了。有人可以阐明这是如何完成的吗?这些助手 classes 将与 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);
        }
    }
}

您是否有对 ServiceLibrary 程序集的引用,其中 MailHelper 在您的项目中?您尝试在项目中使用 using ServiceLibrary; 语句吗?这些是您收到错误的最可能原因。

此外,我从您的代码中看到了一些东西。首先,SendMailMessage 被标记为 static,但您试图将其称为实例方法:mailHelper.SendMailMessage();

其次,SendMailMessage 有参数,其中 none 是在调用中提供的。

对于第一个问题,您可以像这样调用静态方法(带参数):

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

其中 from、to、bcc、cc、subject 和 body 是具有您要使用的值的变量。

或将方法更改为实例方法(删除static关键字):

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

根本原因有两个。第一个问题是对该库的引用丢失,导致我无法正确调用 class。一旦我修复了参考然后使用

using ServiceLibrary.SmtpHelperClass;

它允许我调用 SmtpMail 帮助函数以及访问库的其他方法。