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