【发布时间】:2017-05-29 00:36:17
【问题描述】:
早上好。我一直想弄清楚如何从同一个帮助类的另一部分调用一个帮助类中的函数?我拥有的助手类正在尝试使用 SMTP 发送助手类。在我深入编码之前,我想确保可以做到这一点。
助手类 A 是我的邮件发件人助手 帮助类 B 在确定谁应该接收电子邮件后发送电子邮件警报。
这就是我到目前为止尝试做的事情。我在 B 类中为 A 类设置了 using 子句。
我的印象是我可以像这样简单地调用帮助类来创建一个对象:
ServiceLibrary.SmtpHelperClass smtp = new SmtpHelperClass();
当我尝试使用 smtp.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);
}
}
}
【问题讨论】:
-
错误是什么?它可能与类本身无关。
-
我把它写成这样用于测试: class AuditReminders { SmtpHelperClass mailHelper = new SmtpHelperClass(); mailHelper.SendMailMessage();它说 mailHelper 在当前上下文中不存在。
-
怎么写的?如果需要,您可以编辑您的问题以添加更多信息和代码。
-
我在上面的 Visual Studio 中表示,在调试期间,收到一条消息说:“SendMailMessage 在此上下文中不存在”
标签: c# .net class-library