-2

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);
        }
    }
}
bbcompent1
  • 494
  • 1
  • 10
  • 25
  • What is the error? It may have nothing to do with the class itself. – Tim Jan 13 '17 at 15:36
  • I wrote it out like this for a test: class AuditReminders { SmtpHelperClass mailHelper = new SmtpHelperClass(); mailHelper.SendMailMessage(); } It says mailHelper does not exist in current context. – bbcompent1 Jan 13 '17 at 16:01
  • Wrote it like how? You can edit your question to add more information and code if needed. – Tim Jan 13 '17 at 16:01
  • I stated that above in visual studio during debug, got a message that said: "SendMailMessage does not exist in this context" – bbcompent1 Nov 21 '18 at 12:21

2 Answers2

1

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))
Tim
  • 28,212
  • 8
  • 63
  • 76
  • Actually I did figure it out, it was the way I was trying to call the process. Also, for some bizarre reason, the main project lost the reference to the helper library. All good now, thanks :) – bbcompent1 Jan 20 '17 at 18:07
0

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.

bbcompent1
  • 494
  • 1
  • 10
  • 25