简体   繁体   中英

Windows Service calling helper class inside class library

Good morning. I'm stuck trying to figure out how can I call a function inside a helper class from another part of the same helper class? The helper class I have is trying to use an SMTP Sending Helper class. Before I get too far into coding, I want to make sure this can be done.

Helper class A is my mail sender helper Helper Class B sends out email alerts after determining who should receive the emails.

So here is what I tried to do so far. I set up a using clause for class A inside class B.

I was under the impression I could simply call the helper class like this to create an object:

        ServiceLibrary.SmtpHelperClass smtp = new SmtpHelperClass();

When I try to use smtp.SendMail(...); it errors out. Can someone shed some light on how this is done? These helper classes will work with a windows service. My plan is to call the other helpers based on the scheduled run time.

The caller code is written like so:

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

I get an error saying that SendMailMessage does not exist in this context. My SmtpHelperClass is written like so:

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

Do you have a reference to the ServiceLibrary assembly where MailHelper is in your project? Do you have a using ServiceLibrary; statement in the project you're trying to use it in? Those are the most likely reasons for the error you're getting.

Additionally, there are a couple of things I see with your code. First, SendMailMessage is marked as static , but you're trying to call it like it's an instance method: mailHelper.SendMailMessage();

Secondly, SendMailMessage has parameters, none of which are supplied in the call.

For the first issue, you can call the static method (with the parameters) like this:

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

where from, to, bcc, cc, subject and body are variables with the values you want to use.

Or change the method to be an instance method (remove the static keyword):

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

The root cause was two fold. The first issue was the reference to that library was lost which led to my failing to correctly call the class. Once I fixed the reference and then used

using ServiceLibrary.SmtpHelperClass;

which allowed me to call the SmtpMail helper function as well as accessing the libraries other methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM